Jack Clemmey
Jack Clemmey

Reputation: 1

How can i get this loop to keep repeating until the right number is entered

so im having an issue with my basic code as i cant get it to give me a second output. Basically, i have a code where i need to type a number between 1 and 20. if the input is outside of that range then i have got it to let me try again. the issue is, that when i try to input for the second attempt, it just ends the code after the input rather than giving feedback.ni will show u my code here.

number =  int (input ("Write a number between 1 and 20"))

if number > 20:
    input ("Please input again")


elif number < 1:
    input ("Please input again")

else:
    print ("thank you")

Upvotes: 0

Views: 37

Answers (2)

Fitra Zul Fahmi
Fitra Zul Fahmi

Reputation: 123

You must using loop and condition. You can try this code...

number=int(input("Write a number between 1 and 20: "))

while (number>20 or number<1):
  number=int(input("Please input again: "))
  if number>=1 and number<=20:
    print ("Thank you")
    break   

Upvotes: 2

Tobias Geiselmann
Tobias Geiselmann

Reputation: 2476

You can do an infinite loop and stop it, when you get the right input. Like this:

While True:

    number =  int (input ("Write a number between 1 and 20"))

    if number > 20:
        input ("Please input again")


    elif number < 1:
        input ("Please input again")

    else:
        print ("thank you")
        break

Upvotes: 1

Related Questions