從英語翻譯到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)')
現在,你說你要收集所有的Ups
和Downs
並在最後打印出來。您可以簡單地創建一個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)
使用while循環,在這休息 – zhqiat
你需要尋找到循環,例如'while while循環 – AChampion
有什麼問題?您剛剛描述了您想要創建的循環類型:直到用戶輸入零爲止纔讀取輸入的循環。 – ForceBru