0
for chance in range(3):
  print("Try #"+str(chance+1))
  guess = int(input("Please enter your number "))
  answer = 2
  if guess == answer:
      print("correct!")
  else:
      print("try again")

if someone types a string of letters instead of numbers, how do I deal with that so that the loop isn't broken with an error

  • Do you know about `try`/`except`? And what do you want to happen when letters are entered? – Carcigenicate Oct 20 '20 at 22:59
  • I haven't gotten to try/except. I've been researching but I can't find what I want. I'd like the error message to be skipped and a message asking for a number to be given at the beginning of the loop. – AleeSarion Oct 20 '20 at 23:27
  • I realized that I could use the type function but then the else statement will come at the end of the for loop and that will skip it making it usless – AleeSarion Oct 20 '20 at 23:28
  • `type` won't work, since `input` always returns a string. – Carcigenicate Oct 20 '20 at 23:29
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Carcigenicate Oct 20 '20 at 23:29
  • No...Thought it did but I tried and I can't integrate the idea into my for loop – AleeSarion Oct 20 '20 at 23:50
  • That answer didn't but I found another linked to it that did. Thank alot. – AleeSarion Oct 21 '20 at 00:09

0 Answers0