2014-10-12 145 views
0

我目前在我的大學初學者的Python課程。我的作業部分是創建一個類似着名兒童歌曲「99瓶流行音樂」的節目。任務是:Python上的作業,麻煩與循環

  1. 用戶輸入一個項目。
  2. 該程序打印出如下陳述:「在牆上彈出x瓶,彈出x瓶,取下一個,通過它,(x-1)瓶在牆上彈出」直到它達到0 一旦達到0,程序必須停下來,並說牆上沒有瓶子彈出。
  3. 上限爲99。如果用戶輸入任何超過99,則顯示一個錯誤並迫使計數器開始在99

有道理正確?所以,這裏是我的代碼:

#Bottles of Pop on the Wall 
#User inputs a number to start with 
#Program returns with singing the popular song down to 0 
# 

print("Bottles of Pop on the Wall Program") 

userBottle = int(input("How many bottles? ")) 
bottleCount = userBottle 

while bottleCount > 1: 
     newBottle = userBottle - 1 
     bottleCount -= 1 

    if bottleCount > 99: 
     print("ALERT: no more than 99 bottles allowed, reverting to 99 bottles") 

     userBottle = 99 
     bottleCount = 99 
     newBottle = 99 


    print(userBottle , "bottles of pop on the wall, ", userBottle , "bottles of pop" , 
     "\ntake one down, pass it around, " , newBottle , "bottles of pop on the wall") 
    userBottle -= 1 

if bottleCount == 1: 
    print(userBottle , "bottle of pop on the wall, ", userBottle , "bottle of pop" , 
     "\ntake one down, pass it around, " , "no bottles of pop on the wall") 



input("\nThank you for playing! Press Enter to exit.") 

因此,如果用戶輸入任何低於100的數字,程序將完美工作。但是,如果用戶輸入超過99的任何東西,那就是我遇到問題的地方。

會發生什麼是循環將運行到1,但不會結束。它會重複一次,它會返回:

1 bottles of pop on the wall, 1 bottles of pop 
take one down, pass it around, 0 bottles of pop on the wall 
0 bottle of pop on the wall, 0 bottle of pop 
take one down, pass it around, no bottles of pop on the wall 

Thank you for playing! Press Enter to exit. 

顯然這是不正確的。我的循環出了什麼問題,所以我可以確保當用戶輸入大於99的數字時不會發生這種情況?

非常感謝,我非常感謝您的幫助!

+6

當我還是個孩子的時候,這是瓶啤酒...... – 2014-10-12 04:06:50

+0

如果count> 99並且userBottle變爲99,那麼newBottle是不是98? – csmckelvey 2014-10-12 04:08:36

+0

首先,我將把n> 99的檢查移出循環。一旦你檢查過了,它再也不會是假的了。我相信這也能修復你的bug – 2014-10-12 04:09:27

回答

0

你應該只使用一個變量來跟蹤瓶

print("Bottles of Pop on the Wall Program") 

bottle_count = int(input("How many bottles? ")) 

if bottle_count > 99: 
    print("ALERT: no more than 99 bottles allowed, reverting to 99 bottles") 
    bottle_count = 99 

while bottle_count > 1: 
    print(bottle_count , "bottles of pop on the wall, ", bottle_count , "bottles of pop") 
    bottle_count -= 1 
    print("take one down, pass it around, ", bottle_count, "bottles of pop on the wall") 

print(bottle_count , "bottle of pop on the wall, ", bottle_count , "bottle of pop") 
print("take one down, pass it around, no bottles of pop on the wall") 

input("\nThank you for playing! Press Enter to exit.") 

對於下一步,您應該使用的格式字符串和.format方法的數量。例如。

print("{bottle_count} bottles of pop on the wall, " 
     "{bottle_count} bottles of pop".format(bottle_count=bottle_count)) 
+0

這是完美的!我忘記提及練習要求我至少使用一個「while」循環。這不僅解決了錯誤,而且比我自己的代碼更有效率。非常感謝! – Tyler 2014-10-12 18:08:17

1

看起來你有一個別名問題。

在循環之外,你要求用戶輸入一個數字,比如說他們輸入101,然後你設置bottleCount = userBottle。然後,在循環中,你的userBottle值重置爲99,但注意以下事項:

In [6]: userBottle = 101 

In [7]: bottleCount = userBottle 

In [8]: userBottle = 99 

In [9]: userBottle 
Out[9]: 99 

In [10]: bottleCount 
Out[10]: 101 

以下是爲你的程序。您可能已更改了userBottle值,但尚未更改bottleCount值。

要做的事情是編寫一個函數來以受控方式獲取userBottle值,並且函數只是返回itslef,直到值正確爲止。一個例子可能是:

def get_user_bottles(input_message, error_message1, error_message2): 
    #This makes sure the user inputs a number 
    try: 
     userBottle = int(raw_input(input_message)) 
    except ValueError: 
     print error_message1 
     return get_user_bottles(input_message, error_message1, error_message2) 
    #This makes sure the user inputs a number between 1 and 99 
    if userBottle not in range(1, 100): 
     print error_message2 
     return get_user_bottles(input_message, error_message1, error_message2) 
    else: 
     return userBottle 

userBottle = get_user_bottles('How many bottles?', 
           'The value must be an integer between 1 and 99', 
           'That value is out of bounds, it must be between 1 and 99') 
0

這可以避免循環瓶子。它使用列表理解來代替:

n = int(input("How many bottles? ")) 
if n > 99: 
    print("Sorry. You cannot afford %s bottles. Starting with 99." % n) 
    n = 99 
song = '\n'.join('%s bottles of pop on the wall, %s bottles of pop\ntake one down, pass it around, %s bottles of pop on the wall' % (i,i,i-1) for i in range(n, 0, -1)) 
print(song + "\nno bottles of pop on the wall, no bottles of pop\n") 
input("Thank you for playing! Press Enter to exit.") 

輸出示例:

How many bottles? 2 
2 bottles of pop on the wall, 2 bottles of pop 
take one down, pass it around, 1 bottles of pop on the wall 
1 bottles of pop on the wall, 1 bottles of pop 
take one down, pass it around, 0 bottles of pop on the wall 
no bottles of pop on the wall, no bottles of pop 

Thank you for playing! Press Enter to exit. 

我把修改歌曲的最後一行的自由。在你有0瓶的流行音樂之後,我不認爲有必要從牆上再拿下一個。如果您願意,可以輕鬆將其更改回來。

0

簡單的改變: if bottleCount == 1:

這樣: if userBottle == 1:,因爲這是你在遞減每一步的變量。