2014-10-17 93 views
1

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

这是什么意思?

+0

你有什么问题?这是不是运行(我看到格式错误),它会给你错误的答案?你使用的是什么版本的Python? – munk 2014-10-17 02:13:35

+0

欢迎来到Stack Overflow。请注意我更新问题的方式,以便更适合StackOverflow:1)使用正确的语法。这不是你的电话。 2)陈述确切的问题是什么。 3)使用描述你正在遇到的问题类型的标题,而不是你试图实现的任务。你的问题与计算时间无关。 – GreenAsJade 2014-10-17 02:24:51

+0

你从哪里得到这个? '如果秒> = 60 [秒]/60:' – emnoor 2014-10-18 18:20:48

回答

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

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) 
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)"