我是新手,也是python的初学者,所以请帮助我。我已经完成了以下操作来创建一个程序,该程序告诉用户输入的数字是Up,Down还是输入的第一个数字相同。我的问题是,如何为它创建一个循环,以便用户可以输入尽可能多的数字,然后在输入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 :(得分: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()
您还可以改进一些事项。例如,您不需要在print
之前使用input
语句。你可以简单地做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)
答案 1 :(得分: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
答案 2 :(得分:-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')