0

So I originally wrote this code as a bunch of if/elif/else statements to gain points in order to figure out who a person was. The problem was I couldn't figure out how to make the else statement go back to the original question, as that question was only represented by a variable = raw_input statement. The idea was to make it intricate for a lot of my friends. In fixing the original problem with the else statement, I messed up the addition portion to determine the answer. How do I fix it to actually count as it goes? As you can see I'm extremely new to coding. I'm sorry for the basic question, as I'm sure the answer is very simple. I appreciate any help.

from sys import exit


Tom = 0
Jon = 0
Chris = 0


def question_a():
    q1 = raw_input("Hello there. I am going to try to guess who you are. Would you like to play?")  

    if q1 == "yes":
        question_b()
    elif q1 == "no":
        print "Well f**k you too then"
    else:
        print "You should follow the rules."
    question_a()

def question_b():
    print "Do you have any hair?"

    q1 = raw_input("> ")

    if q1== "no": 
        print "you're Karl" 
        exit(0)


    elif q1 == "yes":
        Tom == Tom + 1
        Chris == Chris + 1
        Jon == Jon + 1
        question_c()
    else:
        print "You should follow the rules."
    question_b()

def question_c():
    print "Do you enjoy working on cars?"

    q1 = raw_input("> ")

    if q1 == "yes":
        Chris == Chris + 1
        Jon == Jon + 1
        question_d()

    elif q1 == "no":
        Tom == Tom + 1
        question_d()
    else: 
        print "you should follow the rules."
    question_c()

def question_d():
    print "Do you own a husky?"

    q1 = raw_input("> ")

    if q1 == "no":
        Tom == Tom + 1
        Chris == Chris + 1      
    elif q1 == "yes":
        Jon == Jon + 1 
    else:
        print "Hey you, follow the rules."
    question_d()

    # python guess_who2.py

    for Tom > Jon and Tom > Chris:
        print "You're Tom"
    for Jon > Chris and Jon > Tom:
        print "You're Jon"
    for Chris > Tom and Chris > Jon:
        print "You're Chris" 
question_a()
2
  • 2
    You've messed up far more than that. This program does not run, due to syntax errors. I recommend incremental programming: write a few lines at a time. Get those working before you write more. Continue this until you get to a problem you can't solve. Commented Feb 12, 2016 at 1:39
  • 2
    You have used == instead of = for assignment. Your counters need to be global variables, not local. You're making recursive calls (a function calling itself) for no particular reason. You really need to fix these things before you can worry about your counting logic. At the end, you use the keyword for to drive a decision; those should be more if statements. Commented Feb 12, 2016 at 1:41

2 Answers 2

1

I hope you are enjoying the world of programming :) I've made some changes to your code in order to make it work, I'll comment them out.

from sys import exit


Tom = 0
Jon = 0
Chris = 0


def question_a():
    q1 = raw_input("Hello there. I am going to try to guess who you are. Would you like to play?")

    if q1 == "yes":
        question_b()
    elif q1 == "no":
        print "Well f**k you too then"
    else:
        print "You should follow the rules."
        question_a()

def question_b():
    global Tom, Chris, Jon
    print "Do you have any hair?"

    q1 = raw_input("> ")

    if q1== "no": 
        print "you're Karl"
        exit(0)


    elif q1 == "yes":
        Tom = Tom + 1
        Chris = Chris + 1
        Jon = Jon + 1
        question_c()
    else:
        print "You should follow the rules."
        question_b()

def question_c():
    global Tom, Chris, Jon
    print "Do you enjoy working on cars?"

    q1 = raw_input("> ")

    if q1 == "yes":
        Chris = Chris + 1
        Jon = Jon + 1
        question_d()

    elif q1 == "no":
        Tom = Tom + 1
        question_d()
    else: 
        print "you should follow the rules."
        question_c()

def question_d():
    global Tom, Chris, Jon
    print "Do you own a husky?"

    q1 = raw_input("> ")

    if q1 == "no":
        Tom = Tom + 1
        Chris = Chris + 1
    elif q1 == "yes":
        Jon = Jon + 1
    else:
        print "Hey you, follow the rules."
        question_d()

# python guess_who2.py
question_a()
if Tom > Jon and Tom > Chris:
    print "You're Tom"
elif Jon > Chris and Jon > Tom:
    print "You're Jon"
elif Chris > Tom and Chris > Jon:
    print "You're Chris"

In each of the functions (question_a(), question_b()..) the call to the function should be indented inside the else statement in order to ask again just in case the answer is not yes neither no.

else:
    print "You should follow the rules."
    question_a()

In the functions where you want to modify the Tom, Chris or Jon variables you need to indicate Python that by using the line

global Tom, Chris, Jon

When you want to increase the points of a variable you do it like this:

Tom = Tom + 1
Chris = Chris + 1
Jon = Jon + 1

because if you write something like

Tom == Tom + 1

you are making a Boolean expression that will give you True or False.

Finally

question_a()
if Tom > Jon and Tom > Chris:
    print "You're Tom"
elif Jon > Chris and Jon > Tom:
    print "You're Jon"
elif Chris > Tom and Chris > Jon:
    print "You're Chris"

We start the execution by calling the question_a() function (as you already know) and check the points using if expressions. The for statements are used to make loops (repeating a piece of code X amount of times)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I should be able to adust the code later today and research the changes to understand them better in the future. I appreciate it.
Awesome, have luck and enjoy!
0

Do it like this:

from sys import exit


def question_a(tom=0, jon=0, chris=0):
    q = ''
    while q not in ['yes', 'no']:
        q = raw_input("Hello there. I am going to try to guess who you are. Would you like to play?")

        if q == "yes":
            question_b(tom, jon, chris)
        elif q == "no":
            print "Well f**k you too then"
        else:
            print "You should follow the rules."


def question_b(tom=0, jon=0, chris=0):
    print "Do you have any hair?"

    q = ''
    while q not in ['yes', 'no']:
        q = raw_input("> ")

        if q == "no":
            print "you're Karl"
            exit(0)
        elif q == "yes":
            tom = tom + 1
            chris = chris + 1
            jon = jon + 1
            question_c(tom, jon, chris)
        else:
            print "You should follow the rules."


def question_c(tom=0, jon=0, chris=0):
    print "Do you enjoy working on cars?"

    q = ''
    while q not in ['yes', 'no']:
        q = raw_input("> ")

        if q == "yes":
            chris = chris + 1
            jon = jon + 1
            question_d(tom, jon, chris)

        elif q == "no":
            tom = tom + 1
            question_d(tom, jon, chris)
        else:
            print "you should follow the rules."


def question_d(tom=0, jon=0, chris=0):
    print "Do you own a husky?"

    q = ''
    while q not in ['yes', 'no']:
        q = raw_input("> ")

        if q == "no":
            tom = tom + 1
            chris = chris + 1
        elif q == "yes":
            jon = jon + 1
        else:
            print "Hey you, follow the rules."

    # python guess_who2.py

    if tom > jon and tom > chris:
        print "You're tom"
    if jon > chris and jon > tom:
        print "You're jon"
    if chris > tom and chris > jon:
        print "You're chris"

    if raw_input('Play again? (yes or no)') == 'yes':
        question_a()
    else:
        print 'bye.'

if __name__ == '__main__':
    question_a()

Try to read some basic syntax on Python first, so that you can handle the if-statement easily.

1 Comment

And thank you as well! I'll check out the differences between your code and melalonso's and try to understand them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.