Reputation: 13
I have to write code for a guessing game and I cannot use loops. I must make the computer choose a random number between 1 and 32 which I have done, the user must make guesses and try get this number. After the user has correctly got the number how do I write some code that stops the game? You only get 5 tries. Here is my code:
import random
random.seed()
number = round((random.random()*32)+0.5)
def G():
if guess > number:
print("Lower...")
else:
print("Higher...")
return False
def W():
print("Correct!")
print("You win!")
return True
guess = int(input("Take a guess"))
if guess==number:
W()
else:
G()
guess = int(input("Try again"))
if guess==number:
W()
else:
G()
guess = int(input("Try again"))
if guess==number:
W()
else:
G()
guess = int(input("Try again"))
if guess==number:
W()
else:
G()
guess = int(input("Try again"))
if guess==number:
W()
else:
print("You lose!")
Upvotes: 0
Views: 378
Reputation: 338
When they say it cannot use loops, does that include recursive functions?
import random
random.seed() # redundant if you don't give it a seed value
number = random.randint(1,32) # This is the more appropriate function if you want integers
def make_guess(): # use descriptive function names
guess = int(input("Take a guess"))
if guess > number:
print("Lower...")
elif guess < number:
print("Higher...")
return guess
def next_round(tries = 0):
tries += 1
guess = make_guess()
if guess == number:
print("Correct!\nYou win!")
return
elif tries >= 5:
print("You lose!")
return
else:
print("Try again")
next_round(tries)
next_round()
I would also add some error handling in case the user enters a non-numeric input. You could make it end the game gracefully with a message telling them to use numbers, or even better, make it so they get that message and the game continues without consuming one of their tries. But I will leave that to you.
Upvotes: 0
Reputation: 3219
You should use recursive function so you can define the number of try to whatever value you want not having to duplicate lines :
def findNumber(numToFind,nbTries):
guess = int(input("Take a guess : "))
if guess == numToFind:
return True
if guess > numToFind:
print("Lower...")
else:
print("Higher...")
if nbTries <= 1:
return False
return findNumber(numToFind,nbTries-1)
result = findNumber(number,5)
if result:
print("Correct!")
print("You win!")
else:
print("You loose:")
Upvotes: 1
Reputation: 3379
I would write as follows - when you start copy-and-paste activity of parts of your code, something is not right:
def G(guess):
if guess > number:
print("Lower...")
else:
print("Higher...")
return False
def W():
print("Correct!")
print("You win!")
return True
def take_a_guess(guess, attempt_number = 1, max_attempts = 5):
if guess == number:
W()
return
else:
G(guess)
if attempt_number == max_attempts:
print("You lost")
return
else:
attempt_number += 1
guess = int(input("Try again: "))
take_a_guess(guess, attempt_number, max_attempts)
MAX_NUMBER_ATTEMPTS = 5
guess = int(input("Take a guess: "))
take_a_guess(guess, 1, MAX_NUMBER_ATTEMPTS)
Upvotes: 0
Reputation: 26
You can use exit()
to stop script execution.
You could call exit(0)
to exit the script showing no errors.
Upvotes: 0