Forums

Guess # Game in 10 attempts error

Hey Guys, anybody know why when i run the program, instead of actually giving me 10 attempts, it is letting me take 12? The program works, and i think it has something to do with the fact that programs read 0 as 1 and therefore, there would be 11 guesses + the original guess that i am giving them...

import random

print("Hello, and welcome, what is your name?")

username = input()

print("We are going to try and guess a number between 0 and 100")

print("You have ten tries until you loose.")

def guess (number_to_guess):

Number = int(input((username + ", what do you think the number is? ")))

attempts = 0

while attempts <= 10:

if Number < number_to_guess:

Number = int(input(("Too low, try again!")))

attempts = attempts + 1

if Number > number_to_guess:

Number = int(input(("Too high, try again!")))

attempts = attempts + 1

elif Number == number_to_guess:

print("Good job! You guessed the number that I was thinking!" + username) break

if Number != number_to_guess:
    print("Too bad, you did not guess the correct number in 10 attempts")
    print("The number that I was thinking of was" + str(answer))

answer = random.randint( 0, 100 )

calling the function and the while loop

guess (answer)

You're telling it to allow 10 guesses. - including a zeroth one Try the counting on your fingers, possibly with fewer allowed tries so that you don't spend forever doing it.

And for reference, programs do not "read 0 as 1", programs read 0 as 0 and 1 as 1. You're getting confused between counting numbers (start at 1 like you learn to count in school) and integers (start at zero). In your program, you've chosen to start at 0.

Yes, you are right Glenn, thank you. My teacher wrote down on the whiteboard what it should look like and that ended up being incorrect. he did that on purposes... lol. a little sneaky. I knew it had to do something with that. Thanks Glenn!

My program is now working! :)

Heh. That's good teaching. Writing code is easy. Working out why it's not doing what you expect is hard.

Lol, yes, i agree. He's great, and also patient which you need when teaching programming! and let me clarify, he didn't actually put the code up there verbatim, he wrote it in English as to what it might look like in code.

But yes, still good teaching