Struggling with the flow of my code. layout issues


#1

I'm making a Guess Who game.
I've made a flowchart and I now need to make a Python program based on the flowchart.
The game has 5 names and 3 questions. 1 of the questions will be asked twice depending on the route taken.

The questions are:
'does the person have a beard?'
'does the person have wrinkles?'
'does the person have a moustache?'

depending on which route I take the question 'does the person have a moustache?' can be asked
twice, so can give 2 different answers depending on the questions asked before it. I have had a little help from a kind person on this site but still struggling a little.
please see the code written so far 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 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')
   elif moustacheIn == 'no' or moustacheIn == 'FALSE':
        print ('zik')
   

beardGuess ()

this is working fine for certain questions but coming up with the below error when certain routes are taken.

Traceback (most recent call last):
File "/Users/marclangford/Documents/greatest.py", line 20, in
beardGuess ()
File "/Users/marclangford/Documents/greatest.py", line 8, in beardGuess
if wrinklesIn == 'yes' or wrinklesIn == 'TRUE':
UnboundLocalError: local variable 'wrinklesIn' referenced before assignment

any help would be greatly appreciated.


#2

The line

moustacheIn = input('does the person have a moustache?')

is only reached if the persone has a beard. So if the person doesn't have a beard moustacheIn is not defined.

However the line

if moustacheIn == 'yes' or moustacheIn == 'TRUE':

is always reached. If moustacheIn is not defined this causes an error.


#3

eveat thank you I really appreciate the help.
I have now edited and runs through every question and gives the correct answer for all 5 names.
The only problem I'm having now is although its running through fine if I go down the route:
'does the person have a beard' == no
'does the person have a moustache' == no
I'm getting the correct name but also an error message below saying:

Traceback (most recent call last):
File "/Users/marclangford/Documents/greatest.py", line 20, in
beardGuess ()
File "/Users/marclangford/Documents/greatest.py", line 15, in beardGuess
if moustacheIn == 'yes' or moustacheIn == 'TRUE':
UnboundLocalError: local variable 'moustacheIn' referenced before assignment

If I go any other route giving any other name other than 'jojohn' I don't receive that message.


#5

please use markup:

otherwise, indent won't show on the forum, this would be problematic for other people trying to help you


#6

 def beardGuess ():
   beardIn = input('does the person have a beard?')
   if beardIn == 'yes' or beardIn == 'TRUE':
      moustacheIn = input('does the person have a moustache?')
      if moustacheIn == 'yes' or moustacheIn == 'TRUE':
            print ('cheska')
      elif moustacheIn == 'no' or moustacheIn == 'FALSE':
            print ('zik')
   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')

beardGuess ()

#7

i know i said i would keep to the sideline, but you keep making the same mistakes. You really need to take some time to understand program flow.

look here:

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

only if elif is true, moustacheIn gets defined, so you would need to nest the choices for moustacheIn inside the elif clause:

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')

#8

Really appreciate the help stetim94.
working perfectly now. I think more revision is required for future projects.


#9

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