2017-05-03 68 views
3

我是新來的,並且是python的初學者,所以請幫助我。我已經完成了以下步驟來創建一個程序,告訴用戶輸入的號碼是從輸入的第一個數字開始,結束還是相同。我的問題是,如何爲它創建一個循環,以便用戶可以輸入儘可能多的數字,然後在輸入0時結束該過程?循環一個代碼並按0結束循環


print ('Please enter your first number') 
firstNumber = input() 

print('Enter the next number(0 to finish)') 
nextNumber=input() 

if firstNumber<nextNumber: 
    print ('Up') 

if firstNumber>nextNumber: 
    print ('Down') 

if firstNumber==nextNumber: 
    print ('Same') 

謝謝

+0

使用while循環,在這休息 – zhqiat

+0

你需要尋找到循環,例如'while while循環 – AChampion

+0

有什麼問題?您剛剛描述了您想要創建的循環類型:直到用戶輸入零爲止纔讀取輸入的循環。 – ForceBru

回答

2

從英語翻譯到Python,你想要的是

while nextNumber != 0: 
    ...keep looping... 

因此,在不改變你的代碼太多,你可以簡單地做:

print ('Please enter your first number') 
firstNumber = input() 

print('Enter the next number(0 to finish)') 
nextNumber=input() 

while nextNumber != 0: 

    if firstNumber<nextNumber: 
     print ('Up') 

    if firstNumber>nextNumber: 
     print ('Down') 

    if firstNumber==nextNumber: 
     print ('Same') 

    # turn the nextNumber into the firstNumber 
    firstNumber = nextNumber 

    print('Enter the next number(0 to finish)') 
    nextNumber=input() 

也有可以改進的幾件事情。例如,您不需要在input之前使用print聲明。你可以簡單地做input('Please enter your first number')。此外,您可以使用if - else而不是僅使用if

這裏是它會是什麼樣子的例子:

firstNumber = input('Please enter your first number') 

nextNumber=input('Enter the next number(0 to finish)') 

while nextNumber != 0: 

    if firstNumber<nextNumber: 
     print ('Up') 

    elif firstNumber>nextNumber: 
     print ('Down') 

    elif firstNumber==nextNumber: 
     print ('Same') 

    # turn the nextNumber into the firstNumber 
    firstNumber = nextNumber 

    nextNumber=input('Enter the next number(0 to finish)') 

現在,你說你要收集所有的UpsDowns並在最後打印出來。您可以簡單地創建一個list並將該值附加到list。像這樣:

ups_and_downs = [] 

firstNumber = input('Please enter your first number') 

nextNumber=input('Enter the next number(0 to finish)') 

while nextNumber != 0: 

    if firstNumber<nextNumber: 
     ups_and_downs.append('Up') 
     print ('Up') 

    elif firstNumber>nextNumber: 
     ups_and_downs.append('Down') 
     print ('Down') 

    elif firstNumber==nextNumber: 
     ups_and_downs.append('Same') 
     print ('Same') 

    # turn the nextNumber into the firstNumber 
    firstNumber = nextNumber 

    nextNumber=input('Enter the next number(0 to finish)') 

print(ups_and_downs) 
+1

謝謝!你可以告訴我,在用戶輸入完所有數字後,如何結束,結果Up,Up,Down ....在最後顯示在一起? –

1
nextNumber = 1 #define 
while nextNumber != 0: #as long as its not 0, run loop 
    print ('Please enter your first number') 
    firstNumber = int(input()) #python treats input as str so cast to int 

    print('Enter the next number(0 to finish)')#cast str to int 
    nextNumber= int(input()) 

    if firstNumber<nextNumber: 
     print ('Up') 

    if firstNumber>nextNumber: 
     print ('Down') 

    if firstNumber==nextNumber: 
     print ('Same') 

輸出:

Please enter your first number 
4 
Enter the next number(0 to finish) 
6 
Up 
Please enter your first number 
7 
Enter the next number(0 to finish) 
8 
Up 
Please enter your first number 
5 
Enter the next number(0 to finish) 
4 
Down 
Please enter your first number 
8 
Enter the next number(0 to finish) 
0 
Down 
+1

非常感謝你,...我可以問有沒有辦法最終收集這些結果?我的意思是在用戶輸入所有數字然後0結束後,結果Up,Up,Down ....一起顯示? –

+0

是的,使用一個列表來存儲每個輸入。看看谷歌如何使用列表。 – Inconnu

-2
firstNumber = input('Please enter your first number ') # ask for the first number 
fn = int(firstNumber) # turn into an integer to compare 

while True: # start a loop 
    nextNumber = input('Enter the next number (0 to finish) ') # ask for another number 
    nn = int(nextNumber) 
    if nn == 0: break # leave the loop if the new number is 0 
    if fn<nn: print('Up') # compare 
    if fn>nn: print('Down') 
    if fn==nn: print('Same') 
+0

如果0 Turtle

+0

您需要首先打破案例這是真的,謝謝! – trotta