While loop user input in range

前端 未结 2 1790
梦如初夏
梦如初夏 2021-01-29 15:55

I have some code that I want to ask the user for a number between 1-100 and if they put a number between these it will print (Size: (input)) and break the loop, if however, they

2条回答
  •  迷失自我
    2021-01-29 16:52

    This should do it.

    c=input("Size: ")
    while int(c)>100 or int(c)<1: 
        #Will repeat until the number is between 1 and 100, inclusively.
        #It will skip the loop if the value is between 1 and 100.
    
        print ("Size: "+c)
        print ("Invalid input. Try again.")
        c=input("Size: ")
    
    #once the loop is completed, the code following the loop will run
    print ("Size: "+c)
    

提交回复
热议问题