Austin Mangelson
Austin Mangelson

Reputation: 43

How do I increases the number of guesses?

I can get everything to work on this code, except when I play the game the result always says it took me 4 guesses. How do I properly increase the number of guesses and relay that back to the player?

import random
n = random.randint(1, 75)
print "I'm thinking of a number between 1 and 75. "
guess = int(raw_input("What's your guess?: "))
while guess != n:
  guesses_taken = 1
  if guess == n:
    break
  if guess < n:
    print "Your guess was too low"
    guess = int(raw_input("Try again:"))
    guesses_taken += guesses_taken
  elif guess > n:
    print "Your guess was too high"
    guess = int(raw_input("Try again:"))
    guesses_taken += guesses_taken
if guess == n:
  guesses_taken += guesses_taken
  guesses_taken = str(guesses_taken)
  print "Good job! It took you " + guesses_taken + " guesses to get it right!"

Upvotes: 1

Views: 57

Answers (1)

C. Fennell
C. Fennell

Reputation: 1032

as @PNX stated, the guesses taken should be outside of the loop, but you're also doing guesses_taken += guesses_taken What you're doing when you do this statement is:

guesses_taken = guesses_taken + guesses_taken

but what you should be doing is :

guesses_taken = guesses_taken + 1

or

guesses_taken += 1

The full code would then be:

import random
n = random.randint(1, 75)
print("I'm thinking of a number between 1 and 75. ")
guess = int(input("What's your guess?: "))
guesses_taken = 1
while guess != n:
  if guess == n:
    break
  if guess < n:
    print("Your guess was too low")
    guess = int(input("Try again:"))
    guesses_taken += 1
  elif guess > n:
    print("Your guess was too high")
    guess = int(input("Try again:"))
    guesses_taken += 1
if guess == n:
  guesses_taken = str(guesses_taken)
  print("Good job! It took you " + guesses_taken + " guesses to get it right!")

I also translated this code to Python 3 because I have no way to run Python 2. You will have to convert back if you still want to use Python 2

Upvotes: 1

Related Questions