Baka Oppai
Baka Oppai

Reputation: 11

Error with my game?

from random import randint

random_number = randint(1, 10)

guesses_left = 3
# Start of the game
while guesses_left != 0:
   def guess():
        guess = int(input("What is your guess?"))
        if guess > 10:
        print ("Insert value between 1 and 10")
        guesses_left += 1
    guess()
if guess == random_number:
    print ("You win")
    break
guesses_left -= 1
else:
    print ("You lose")

I am making this game where random numbers are formed and the user guesses the random number to win the game. However, I want to implement a safety that if the guess goes over 10, then it will print "enter value from 1 to 10" (see the code) and also add 1 to the guess left. I made this into a function so that the print message keeps displaying itself until the user puts all his guesses from 1 to 10. I am getting an error in that function itself :(

Also, how can I keep displaying the print message without functions? I know how to do it with while loop but is there a better way to do it?

Upvotes: 0

Views: 64

Answers (3)

andrewfarah
andrewfarah

Reputation: 453

from random import randint
random_number = 10 # randint(1, 10)
guesses_left = 3

# Start of the game
while guesses_left != 0:
    guess = int(input("Guess a number between 1 and 10: "))
    if guess > 10:
        print "(error: insert a value between 1 and 10)"
        guesses_left = guesses_left - 1 #add if you want to dock them a guess for not following instructions :)
    else:
        if guess is random_number:
            print ("You win!")
            break
        else:
            print "Nope!"
            guesses_left = guesses_left - 1
    if guesses_left is 0:
        print "Wah Wah. You lose."

My code is probably more verbose than it needs to be but it does the trick. There are a couple problems with the way you wrote the script.

  • Like Hack Saw said, I don't think you need the function.
  • Indentation is off. This matters a lot to python

Good luck in class! :)

Upvotes: 0

Brian
Brian

Reputation: 1998

Several notes here:

1) Remove the definition of the guess function outside of the while loop.

2) Watch the indentation. It's meaningful in Python

3) I merged the guess function with the main code, and it's still pretty readable

4) You can avoid having to increment guesses_left by 1 if you don't decrement it

5) I really hope you're trying to learn programming, and not having us complete your homework for you. Python can be very powerful, please continue to learn about it.

from random import randint
random_number = randint(1, 10)

guesses_left = 3

# Start of the game
win=False
while guesses_left>0:
    guess=int(input("What is your guess?"))
    if guess==random_number:
        win=True
        break
    elif guess>10:
        print("Insert value between 1 and 10")
        continue
    else:
        guesses_left -= 1
if win:
    print("You win")
else:
    print("You lose")

Upvotes: 0

Hack Saw
Hack Saw

Reputation: 2781

You don't need the function at all, since you don't call it more than once.

A good way to think about the problem is that the program starts with a state of not knowing what the players guess is. You can represent this as 0. Then the while loop checks to see if the guess is >=1 AND <=20. Since the first time around, it's not, the loop would ask for a guess between 1 and 10.

Upvotes: 0

Related Questions