So I'm working on a school assignment for a simple random number guessing game. I've met most of the needed requirements for the assignment (except for the extra credit, but i'll tackle that later).
Anyways, aside from that what I can't seem to figure out if how to get the game/code to run again so long as the player keeps typing in play. I have been able to get the code to run at least one more time once the user types in play.
My goal however is to get the code/game to keep running until the player selects quit. I'm guessing I'd need to use something along the lines of a While loop to get the job done, instead of the if statement I used.
But i'm not sure how to go about it in a safe way so as to avoiding the game infinitely running/printing as soon as the player types play, which is what i'm assuming is what would happen if I wrote:
while again_or_no == ("play"):
Etc. Essentially, I have two problems, how to safely loop the entirety of my code, and one of my else statements aren't working. Note I'm a beginner, so I realize may code may be inefficient any and all suggestions are welcome :).
Anyways, here's the code I used in my game below:
def game_or_nah():
from random import randint
rand_numb = randint(1, 100)
print (rand_numb)
player_one = (input("Before we get started what is your name?: "))
print("The Myster Number Game Directions:\n----------------------------------------------------------------------------- \n>Hello,",player_one,"this is the Mystery number guessing game where a random number between 0-100 is generated.\n>We'll prompt you to type in how many guesses you think you'll need in order to guess the number.\n>once you run out of guesses, the game will end, and it'll be game over for you.")
def game_format():
return("-----------------------------------------------------------------------------")
print (game_format())
print ("Are you ready to play,",player_one,"?")
play_or_quit = (input("(Play/Quit):"))
if play_or_quit == ("Play") or play_or_quit == ("play") or play_or_quit == ("p"):
numb_turn = (int(input("Alright let's get started,the random number has been generated!\nHow many guesses do you think you'll need?: ")))
print(game_format())
while numb_turn != 0:
guess = int(input("Your guess: "))
if guess == rand_numb:
print ("You win, thanks for playing,",player_one,"!")
print(game_format())
break
if guess > 100 or guess < 0:
print("Your guess isn't even in range,",player_one,"! (1-100)")
elif guess > rand_numb:
print("Too high,",player_one,"!")
numb_turn -= 1
print("You have",numb_turn,"guess(es) left.")
elif guess != rand_numb:
print("Not quite,",player_one,". Too low, Try again.")
numb_turn -= 1
print("You have",numb_turn,"guess(es) left.")
if numb_turn == 0:
print ("Oops! Looks like you're out of guesses!\nGame Over.\nThe mystery number was",rand_numb)
print(game_format())
break
else:
print("Goodbye!")
print (game_format())
This is the else statement that I can't seem to get to work:
else:
print("Goodbye!")
print (game_format())
Here's the error code I'm getting:
Traceback (most recent call last):
File "python", line 38, in <module>
File "python", line 15, in game_or_nah
UnboundLocalError: local variable 'numb_turn' referenced before assignment
I'm guessing this is as a result of the way I have my If/Else statements set up and my code is trying to evaluate numb_turn, but it can't because the 1st if condition wasn't met and therefore numb_turn was never assigned. So I guess that meansI have to move it someplace else in the code but I'm not sure where?
And this is the if statement I'm using to run the game again, don't know why it didn't fit with the rest of the code (only runs it once more):
print(game_or_nah())
again_or_no = (input("Play again?(Y/N): "))
if again_or_no == ("yes") or again_or_no == ("Yes") or again_or_no ==("y") or again_or_no == ("Y"):
print("Loading new game...\n-----------------------------------------------------------------------------")
print("\n"*40)
print(game_or_nah())
else:
print("Goodbye!\n-----------------------------------------------------------------------------")
Apologies in advanced for the great (Well I wouldn't say great) wall of code up there.
Aucun commentaire:
Enregistrer un commentaire