2014-10-17 35 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)"