Cant figure out where I am going wrong


#1

I'm making a Guess Who game flowchart on Python.

I'm getting error codes:
Traceback (most recent call last):
  File "/Users/marclangford/Documents/greatest.py", line 21, in <module>
    beardGuess ()
  File "/Users/marclangford/Documents/greatest.py", line 8, in beardGuess
    if moustacheIn == 'yes' or moustacheIn == 'TRUE':
UnboundLocalError: local variable 'moustacheIn' referenced before assignment

The program is working on certain routes but not on others. I have a Guess who game. The game is small and has 5 names. There are 3 questions 'do they have a beard?', 'do they have a moustache?' and 'do they have wrinkles?' I think the problem I'm having is 'do they have a moustache?' is asked at the start of the flowchart and asked again if the answer if false to 2 of the questions. Please find my code below:

def beardGuess ():

   beardIn = input('does the person have a beard?')
   if beardIn == 'yes' or beardIn == 'TRUE':
        moustacheIn = input('does the person have a moustache?')
   elif beardIn == 'no' or beardIn == 'FALSE':
        wrinklesIn = input('does the person have wrinkles?')
   if moustacheIn == 'yes' or moustacheIn == 'TRUE':
        print ('cheska')
   elif moustacheIn == 'no' or moustacheIn == 'FALSE':
        print ('zik')
   if wrinklesIn == 'yes' or wrinklesIn == 'TRUE':
        print ('jojohn')
   elif wrinklesIn == 'no' or wrinklesIn == 'FALSE':
        moustacheIn = input('does the person have a moustache?')
   if moustacheIn == 'yes' or moustacheIn == 'TRUE':
        print ('sestine')
   elif moustacheIn == 'no' or moustacheIn == 'FALSE':
        print ('aubro')

beardGuess ()
        
Hope I've give enough information and not confusing too much. Very new to python so sorry if its an obvious mistake.

#2

lets say i enter yes for this prompt:

beardIn = input('does the person have a beard?')

then this code execute:

if beardIn == 'yes' or beardIn == 'TRUE':
        moustacheIn = input('does the person have a moustache?')

then the elif is skipped (given if is true, elif will never run) so then when this line is reached:

if wrinklesIn == 'yes' or wrinklesIn == 'TRUE':

you have a problem, since wrinkleIn is defined in the skipped elif, you need to think through the flow


#3

Thank you stetim94
I can see where I've gone wrong. I've swapped it around a bit and if when the answer is yes to ('does the person have a beard?') then it will now ask if they have wrinkles and if answered yes will now take me to ('does the person have a moustache?') but if the answer is yes it is now giving 2 answers of sestine and cheska as the question ('does the person have a moustache?') is asked twice. I'm struggling to figure out how to give 1 answer to that question if 1 route is taken and a different answer if another route is taken. My code is now:

def beardGuess ():

   beardIn = input('does the person have a beard?')
   if beardIn == 'yes' or beardIn == 'TRUE':
        moustacheIn = input('does the person have a moustache?')
   elif beardIn == 'no' or beardIn == 'FALSE':
        wrinklesIn = input('does the person have wrinkles?')
   if wrinklesIn == 'yes' or wrinklesIn == 'TRUE':
        print ('jojohn')
   elif wrinklesIn == 'no' or wrinklesIn == 'FALSE':
        moustacheIn = input('does the person have a moustache?')
   if moustacheIn == 'yes' or moustacheIn == 'TRUE':
        print ('sestine')
   elif moustacheIn == 'no' or moustacheIn == 'FALSE':
        print ('aubro')
   if moustacheIn == 'yes' or moustacheIn == 'TRUE':
        print ('cheska')
   elif moustacheIn == 'no' or moustacheIn == 'FALSE':
        print ('zik')
   

beardGuess ()

#4

why don't you nest wrinklesIn inside beardIn no?

   elif beardIn == 'no' or beardIn == 'FALSE':
      wrinklesIn = input('does the person have wrinkles?')
      if wrinklesIn == 'yes' or wrinklesIn == 'TRUE':
          print ('jojohn')
       elif wrinklesIn == 'no' or wrinklesIn == 'FALSE':
          moustacheIn = input('does the person have a moustache?')

this way, wrinklesIn is only reached if beard is no or false.


#5

I will give that a try, thank you. I still have the problem with moustache giving out 2 names
if beard == no, wrinkless == 'no' and moustache == 'yes' I'm getting the correct answer of 'sestine' but also getting 'cheska' because if beard == 'yes' and moustache == 'yes' i should get the answer 'cheska' so moustache can give out 2 different answers depending on the answers given.
Thank you for your help, its much appreciated.

Sent using the free mail.com iPhone App


#6

i helped you currently with your problem twice, i wasn't planning on keep helping, take some time to think through the flow of the program.


#7

Ok thank you for your help.

Sent using the free mail.com iPhone App


#8

feel free to make a new topic, i am sure someone else will take over


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.