Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop
#1
I'm currently doing a tutorial on while loops, I followed along fine, but I always try to add something of my own to each code he teaches, so my brain has to think rather than just copy, but I'm stuck on something I want to add...

secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
    guess = int(input("Guess: "))
    guess_count += 1
    if guess == secret_number:
        print("You win!")
        break
else:
    print("Fail!")
This is as per the tutorial. As you can see it works fine for numbers input, but at the moment if someone adds a non-numerical character it just responds with an error. I wanted to add something that would give a reply along the lines of "Invalid answer, numbers only please." then revert back to the first guess in the loop.

I realize it's probably quite basic for you guys but it's difficult for me psychologically to move on until I have done it (weirdo), I tried several different ideas and "wasted" a good couple hours on it (at least I've learnt what wont work), but it's possible that it just requires something I haven't learnt yet.

It's my first post, so I'm hoping you guys suffer noobs gladly. Pray
Reply


Messages In This Thread
while loop - by Blob - Nov-12-2019, 11:03 AM
RE: while loop - by perfringo - Nov-12-2019, 11:25 AM
RE: while loop - by jefsummers - Nov-12-2019, 12:21 PM
RE: while loop - by ichabod801 - Nov-12-2019, 12:46 PM
RE: while loop - by Blob - Nov-14-2019, 04:09 PM
RE: while loop - by ichabod801 - Nov-14-2019, 04:45 PM
RE: while loop - by Renym - Nov-15-2019, 01:50 PM
RE: while loop - by jefsummers - Nov-15-2019, 01:59 PM
RE: while loop - by ichabod801 - Nov-15-2019, 02:03 PM
RE: while loop - by the_ophidian_order - Nov-15-2019, 03:42 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020