只要输入大于前一个输入就循环

时间:2019-05-08 11:07:22

标签: python

抱歉-只是一个简单的任务,要求更高的输入,除非它等于或小于先前的输入。 输入值是否需要占位符,还是我错过了休息时间?

num=input("Enter a number: ")
while True:
    num <= num
    print("Something is wrong")
    num=input("Try again: ")
if num >= num:
    print("Keep going higher!")

代码输出

Something is wrong 
Try again

1 个答案:

答案 0 :(得分:2)

import sys

num_tmp = -sys.maxsize - 1 # this expression returns lowest possible number
while True:
    num = input("Enter a number: ") # input returns string value

    if not num.isdecimal(): # checking if input contains only digits
        print("Not a number, try again.")
        continue # skips futher instructions and go to next iteration
    elif int(num) < num_tmp: # int(num) converts string to integer
        print("Entered number is lower that previous. That's all. Bye.")
        break # breaks loop execution ignoring condition

    num_tmp = int(num) # updating num_tmp for comparsion in next iteration
    print("Keep going higher!")