I have about 3 days of learning python from a pdf book on basic games using python. So far I understand what im doing, or at least think I do. I tried to make up an original code using two previous source codes that were used as examples in the book. But unfortunately I keep on getting this error:
File "/home/notion/SourceCode/15.py", line 80, in <module>
File "/home/notion/SourceCode/15.py", line 7, in displayIntro
print('In this game you will have to first guess the correct number')
UnboundLocalError: local variable 'print' referenced before assignment
I understand this must be a really simple error to fix, but seeing as I've only been doing this for 3 days, I'm a bit confused as to what it is I am suppose to change in order for the script to run. I actually tried adding another statement
with a while loop:
playGame = 'play'
while playGame == 'play'
so this could come up after the displayIntro statement. It's stupid but I wanted to add it so the user/player had the option to input "play" after a message like: i.e. print('Do you wish to play (enter "play")') so it can feel a little bit more interactive, but that worked out disastrously, so much so that i took it out of the script.
I'm basically trying to use what ive learned to make something of my own or at least step in a direction to start making my own original scripts, so any help or advice would be great. I really wish to understand and use everything im learning, adecuately.
#Guess a number and choose a door. import random import time def displayIntro(): print('In this game you will have to first guess the correct number') time.sleep(3) print('And then you will be allowed to go into one of 2 doors') time.sleep(3) print('You will have a limit on the amount of guesses you can take') time.sleep(2) print('But only one shot to step into the correct door') print=() print('Game on') def guessingGame(): guessesTaken = 0 print('Hello! What is your name?') myName = input() number = random.randint(1, 10) print('Well, ' + myName + ', lets play a game.') print('I am thinking of a number between 1 and 10.') while guessesTaken < 6: print('Take a guess.') guess = input() guess = int(guess) guessesTaken = guessesTaken + 1 if guess < number: print('your guess is too low.') if guess > number: print('Your guess is too high.') if guess == number: break if guess == number: guessesTaken = str(guessesTaken) print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!') if guess != number: number = str(number) print ('Nope. The number I was thinking of was ' + number) def chooseDoor(): door = '' while door != '1' and door != '2': print('Which door will you choose to go into, fool? (1 or 2)') cave = input() return cave def checkDoor(chosenDoor): print('you approach the cave...') winningDoor = random.randint(1,2) if chosenDoor == str(winningDoor): print('') else: print('') playAgain = 'yes' while playAgain == 'yes' or playAgain == 'y': displayIntro() guessingGame() doorNumber = chooseDoor() checkDoor(chosenDoor)