我是python中的新手程序员,我有这个练习似乎让我和很多其他人一样,我真的很感激一些帮助!
这是问题所在:编写一个程序,要求用户输入几秒钟,并按如下方式工作:
一分钟内有60秒。如果用户输入的秒数大于或等于60,则程序应显示该秒数的分钟数。
一小时有3600秒。如果用户输入的秒数大于或等于3600,则程序应显示该秒数的小时数。
一天有86400秒。如果用户输入的秒数大于或等于86400,则程序应显示该秒数的天数。
到目前为止我的代码:
print('enter a number of seconds')
seconds = int(input('enter a number of seconds'))
if seconds >=60 [seconds] / 60:
if seconds >=3600 [seconds] / 3600:
if seconds >=86400 [seconds] / 86400
我们运行时遇到的问题是:
Traceback (most recent call last):
File "main.py", line 5, in
if seconds >=60 [seconds] / 60:
TypeError: 'int' object is not subscriptable
这是什么意思?
答案 0 :(得分:2)
1)您的程序没有打印您正在计算的数字,因为您没有要求它打印任何内容。
(你没有计算任何东西)
2)你没有远程有效的python语法。
到底是什么
if seconds >=60 [seconds] / 60:
你能大声读出来吗?
我认为你得到的错误消息(这是我运行代码时得到的错误信息,所以我把它放在你的问题中)说:
TypeError: 'int' object is not subscriptable
这是因为thing [other thing]
语法是一个下标操作。
你在做60[seconds]
。这表示“从60阵列中取出秒元素”。 Python无法理解。 60是整数,而不是数组。整数不可订阅。这就是它告诉你的。
你想要这样的东西:
if seconds >= 60: # if seconds is greater than 60
if seconds >= 3600: # and it's greater than 3600
if seconds >= 86400: # and it's greather than 86400
print seconds/86400 # then it's really big so divide by the big number and print
else:
# here, it's less than 86400 and more than 3600
print seconds/3600 # so divide by 3600
else:
# here it's a minute kind of number
print seconds/60
else:
# its less than 60
print seconds
请注意,到目前为止,这并不是最优雅的方式,它只是一些类似于你的逻辑,但具有大致有效的python语法。
请注意,这是python 2.x语法。如果您使用的是python 3.x,请将其作为标记添加到您的问题中。
答案 1 :(得分:0)
欢迎来到编程世界!
我已经创建了一个接近您需要的示例。你应该能够从中得到答案。此处的一些内容对您来说可能有些先进,但如果您按照下面的链接,您可以使用它并从中学习。
运行代码的链接:http://repl.it/1d0
#When you put something after a '#' symbol, it's a comment!
#This lets you explain your code to other people.
#Rather than typing these numbers over and over,
#you should store them in variables so that you can reuse them.
secondsPerMinute = 60
secondsPerHour = 60*secondsPerMinute
secondsPerDay = secondsPerHour*24
#This is a function. Like a function in math, it takes in several values as 'parameters',
#and then returns a value back. This function takes a number and returns its rounded value.
#See if you can figure out how it works.
def round(number):
return int(number + .5)
#This function takes a number and a unit, and uses them to make a sentence.
def say(number, unit):
print "That's {0} {1}s!".format(number, unit)
print "That's {0} {1}s if you round to the nearest number!".format(round(number), unit)
print('Enter a number of seconds:')
seconds = float(input())
#In this series of if-statements, we'll go through and figure out
#the most appropriate unit of time to use, and then store two values
#to use with the say function.
if seconds >= secondsPerDay:
number = seconds / secondsPerDay
unit = "day"
elif seconds >= secondsPerHour:
number = seconds / secondsPerHour
unit = "hour"
elif seconds >= secondsPerMinute:
number = seconds/secondsPerMinute
unit = "minute"
else:
number = seconds
unit = "second"
#We're calling the say function using the variables from above as 'arguments'.
say(number, unit)
答案 2 :(得分:0)
要添加到GreenAsJade的答案,您可以编写这样的条件以避免不必要的嵌套。
if seconds >= 86400:
print seconds/86400, "day(s)"
elif seconds >= 3600:
print seconds/3600, "hour(s)"
elif seconds >= 60:
print seconds/60, "minute(s)"