如何打印和理解Python错误信息
我是一名初学者,正在学习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
这是什么意思呢?
3 个回答
为了补充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)"
欢迎来到编程的世界!
我做了一个例子,跟你需要的东西很接近。你可以从这个例子出发,找到你的答案。这里面有些内容可能对你来说有点复杂,但如果你点击下面的链接,你可以动手试试,从中学习。
运行代码的链接: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)
1) 你的程序没有打印出你计算的数字,因为你没有让它打印任何东西。
(而且你也没有计算任何东西)
2) 你的代码语法完全不符合Python的规则。
那句
if seconds >=60 [seconds] / 60:
你能把它读出来吗?
我猜你看到的错误信息是:
TypeError: 'int' object is not subscriptable
这句话的意思是说,thing [other thing]
这种写法是用来取数组里的元素的。
你写的是60[seconds]
。这就像是在说“从60这个数组里取出seconds这个元素”。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,请在你的问题中加上这个标签。