7 Replies - 299 Views - Last Post: 16 April 2013 - 08:36 PM Rate Topic: -----

#1 Notion  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 14-April 13

Problem executing game script

Posted 15 April 2013 - 11:44 AM

Hello,

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>
displayIntro()
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)





Is This A Good Question/Topic? 0
  • +

Replies To: Problem executing game script

#2 Notion  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 14-April 13

Re: Problem executing game script

Posted 15 April 2013 - 03:38 PM

Update: Ok so I fixed the issue. took out some unecessary statements

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():
        print('Which door will you choose to go into, fool? (1 or 2)')
        door = input()

        return door


def checkDoor(chosenDoor):
    print('you approach the doors...')

    winningDoor = random.randint(1,2)

    if chosenDoor == str(winningDoor):
        print('Congrats you have chosen the winning door!')
    else:
        print('Sorry, you have chosen the loosing door')

            


playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
    
    displayIntro()
    
    guessingGame()
    
    doorNumber = chooseDoor()
    
    checkDoor(doorNumber)

    print()
    print('Do you want to play again')
    playAgain = input()



Was This Post Helpful? 0
  • +
  • -

#3 Notion  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 14-April 13

Re: Problem executing game script

Posted 15 April 2013 - 07:13 PM

OK so now I have a new question relating to the use of the 'and' boolean operator and I would really appreciate ANY reply:
If I wanted to make the while condition loop so if the user/player were to enter any number or letter that is not 1-10, it simply loops and the print function with the "guess a number from 1-10" string will continue popping up until the player types a correct value (1-10). I ask this because say if i wanted to use a big range say from 1-100, it would be a pain to write an and operator for every one, how can i write it so the interpreter understands that it should not break from the while loop until a correct value is enter( in this case a number NOT a letter). I hope my question is clear.
i.e.
variable = ''
while variable != '1 and variable != 2'
print(' ')
variable = input()


 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

Was This Post Helpful? 0
  • +
  • -

#4 Notion  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 14-April 13

Re: Problem executing game script

Posted 15 April 2013 - 09:15 PM

Tried this but I got this error:
Traceback (most recent call last):
File "E:/PythonSourceCodes/DoubleGuess.py", line 84, in <module>
guessingGame()
File "E:/PythonSourceCodes/DoubleGuess.py", line 27, in guessingGame
guess !='1, 2, 3, 4, 5, 6, 7, 8, 9, 10'
UnboundLocalError: local variable 'guess' referenced before assignment

 while guessesTaken < 6:
        guess !='1, 2, 3, 4, 5, 6, 7, 8, 9, 10'
        print('Guess any number between 1 and 10')
        guess = input()
        guess = int(guess)
        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!')

Was This Post Helpful? 0
  • +
  • -

#5 Notion  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 14-April 13

Re: Problem executing game script

Posted 15 April 2013 - 09:23 PM

OK sorry, that was a ridiculous thing to do. i would need to include this in another while condition with the variable !='....' and !='...' expression or find out how to include the condition in the while guessesTaken <6: ..... Not giving up till I can make it work. Its fun trying to figure it out though.

  guess !='1, 2, 3, 4, 5' and guess !='6, 7, 8, 9, 10':
        print('Guess any number between 1 and 10')
        guess = input()
        guess = int(guess)
        guess = ''

Was This Post Helpful? 0
  • +
  • -

#6 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Problem executing game script

Posted 15 April 2013 - 11:57 PM

One way to keep asking for an input (code is for python 3):
guess = None
while guess not in [str(num) for num in range(1,11)]:
    guess = input("Pick a number between 1 and 10: ")

-Mek

This post has been edited by Mekire: 15 April 2013 - 11:59 PM

Was This Post Helpful? 1
  • +
  • -

#7 Notion  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 14-April 13

Re: Problem executing game script

Posted 16 April 2013 - 01:30 PM

If its alright I'd like to break down what you did to see if im understanding it right, im also having trouble integrating code into the def guessingGame statement. Oh and pyscript is definitely better than the IDLE, feels different in a good way.
def guessingGame():
    print('Hello! What is your name?')
    myName = input()

    number = random.randint(1, 10)
    print('Well, ' + myName + ', lets play a game.')

    guess = None
    while guess not in [str(num) for num in range(1,11)]:
        guess = input('Pick a number between 1 and 10:')

so the def guessingGame(): statement will define what will be executed within that def-block.
the code will run until it hits the first while loop( im assuming None is the same value as having typed guess = 0 ?),
where the not operator will evaluate whether the value of the guess is in or not in the [] list brackets.
If it is in(False) it will break to the next part of the script, if it is not(True) it will keep looping until it evaluates false.
the str() function will convert the (num)argument into a string value,
the for loop will loop over the range object int this case(0-10), the num variable im assuming is what the while condition is looping over to see if the guess variable is equal to an integer value between 0-10((range(1,11)) which is evaluated as a string value thanks to the str(num) function?
My question is, im still overwhelmed with confusion, its a lot of info im trying to take in and understand(I started learning python a couple days ago),
What I want to do is for this to run and not crash after the input from the user is stored:
    guess = None
    while guess not in [str(num) for num in range(1,11)]:
        guess = input('Pick a number between 1 and 10:')
 guessesTaken = 0
    while guessesTaken < 6:

            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)

The while statement should evaluate the condition guessesTaken < 6 true since I put guessesTaken = 0.
But when I run it i get this error which i assume has something to do with how I organized the second while statement:
Message File Name Line Position
Traceback
<module> E:\PythonSourceCodes\DoubleGuess.py 75
guessingGame E:\PythonSourceCodes\DoubleGuess.py 30
TypeError: unorderable types: str() < int()
Was This Post Helpful? 0
  • +
  • -

#8 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Problem executing game script

Posted 16 April 2013 - 08:36 PM

An implementation (code is python 3):
from random import randint

def guess_game():
    number = randint(1,10)
    name = input("Please enter your name: ")
    guesses = 0
    while guesses < 6:
        guess = None
        guesses += 1
        while guess not in [str(num) for num in range(1,11)]:
            guess = input("Pick a number between 1 and 10: ")
        guess = int(guess)

        if guess < number:
            print("Your guess is too low.")
        elif guess > number:
            print("Your guess is too high.")
        else:
            print("Good job, {}! You guessed my number in {} guesses!".format(name,guesses))
            break
    else:
        print("Nope. The number I was thinking of was {}".format(number))

if __name__ == "__main__":
    guess_game()

-Mek
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1