gpt4 book ai didi

python - 如何打印以及如何解释 python 错误消息

转载 作者:行者123 更新时间:2023-12-01 05:00:11 25 4
gpt4 key购买 nike

我是一名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

这是什么意思?

最佳答案

1)您的程序没有打印您正在计算的数字,因为您没有要求它打印任何内容。

(并且您没有计算任何内容)

2) 你没有远程有效的 python 语法。

这到底是什么

如果秒 >=60 [秒]/60:

你能大声读给我听吗?

我认为您收到的错误消息(这是我运行您的代码时收到的错误消息,因此我将其放入您的问题中)是这样的:

类型错误:“int”对象不可订阅

这是说因为 thing [other thing] 语法是下标操作。

您正在做60[秒]。这表示“从 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,请将其作为标签添加到您的问题中。

关于python - 如何打印以及如何解释 python 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26416797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com