获取while语句的输入

时间:2014-01-23 19:42:34

标签: python

while int(input("Input an integer (0 terminates): ")) != 0:
    #do stuff to the input

如何存储用户在上面一行中输入的行的输入。

3 个答案:

答案 0 :(得分:1)

我认为最好让你的while循环像这样:

# loop continuously
while True:
    # get the input and store it in the variable inp
    inp = int(input("Input an integer (0 terminates): "))
    # break the loop if inp equals 0
    if inp == 0:
        break
    # do stuff to the input

答案 1 :(得分:0)

最好和更清洁的解决方案是进行无限循环并在用户输入0时打破它:

while True:
    inp = int(input("Input an integer (0 terminates): "))
    if inp == 0:
        break 
    print inp

答案 2 :(得分:0)

while 1:
    n = input("Input an integer (0 terminates): ")
    if n == 0:
        break
    # do something with n