Jesper
Jesper

Reputation: 141

Loop as long as input is greater then previous input

sorry - just a simple task, of asking for higher input unless its equal or less than previous input. does it require a place holder for the input value or am I missing a break?

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

code output

Something is wrong 
Try again

Upvotes: 0

Views: 537

Answers (1)

Olvin Roght
Olvin Roght

Reputation: 7812

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!")

Upvotes: 2

Related Questions