-2
i=1
for i in range(4):
    i=i+1
    print(i,".number is:")
    a=int(input(":"))
    if a>13:
        print("Try again")

Hello I a am beginner,I have a code like that, I want it to input a number < than 13 , if input >13 then ask again. But it print :

1 .number is:
: 1
2 .number is:
: 2
3 .number is:
: 3
4 .number is:
: 14
Try again
Prune
  • 76,765
  • 14
  • 60
  • 81
  • you want to try and use the while loop – Joe Ferndz Aug 12 '20 at 22:20
  • What? I don't understand. What didn't work, and what do you expect to happen? Please [edit] your question to include these details. See [ask], [tour], [help]. – 10 Rep Aug 12 '20 at 22:20
  • This seems to do what you describe you want it to do. – Scott Hunter Aug 12 '20 at 22:20
  • Are you trying to accept a value from the user. If the user value is less than 13, then you want to break. If the value is > 13 then you want to ask again. In other words, you want user to enter only a value between 1 and12. Correct? – Joe Ferndz Aug 12 '20 at 22:24

1 Answers1

1

You're missing an else clause. Your loop doesn't break because you don't explicitly tell it to, and it'll run until you enter a number over 13.

i=1
for i in range(4):
    i=i+1
    print(i,".number is:")
    a=int(input(":"))
    if a>13:
        print("Try again")
    else:
        break
Makoto
  • 104,088
  • 27
  • 192
  • 230