【问题标题】:How to print and how to interpret python error messages如何打印以及如何解释 python 错误消息
【发布时间】:2014-10-17 02:05:02
【问题描述】:

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

这是什么意思?

【问题讨论】:

  • 您遇到了什么问题?这不运行(我看到格式错误),它会给你错误的答案吗?你用的是什么版本的 Python?
  • 欢迎来到 Stack Overflow。请注意我更新您的问题以更适合 StackOverflow 的方式:1)使用正确的语法。这不是你的手机。 2) 说明确切的问题是什么。 3) 使用描述你遇到的问题类型的标题,而不是你想要完成的任务。你的问题和问题与计算时间无关。
  • 你从哪里得到这个的? if seconds >=60 [seconds] / 60:

标签: python python-3.4


【解决方案1】:

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,请将其作为标签添加到您的问题中。

【讨论】:

    【解决方案2】:

    欢迎来到编程世界!

    我创建了一个接近您需要的示例。您应该能够从中获得答案。目前,这里的一些内容可能对您来说有点先进,但如果您点击下面的链接,您可以玩它并从中学习。

    运行代码的链接: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)
    

    【讨论】:

      【解决方案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)"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-09
        • 2016-08-07
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 2018-08-08
        • 2020-06-13
        相关资源
        最近更新 更多