简体   繁体   English

Python通过if else和def添加

[英]Python adding through if else and def

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. 因此,我最初将此代码编写为一堆if / elif / else语句来获得积分,从而弄清楚一个人是谁。 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. 问题是我不知道如何使else语句回到原始问题,因为该问题仅由变量= raw_input语句表示。 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. 在用else语句解决原始问题时,我弄乱了加法部分以确定答案。 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()

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 . 在每个函数中(question_a(),question_b()..),应在else语句内缩进该函数的调用,以再次询问,以防万一,答案为yes或 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 在要修改Tom,Chris或Jon变量的函数中,需要使用以下行来指示Python:

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. 您正在制作一个布尔表达式,该表达式将为True或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. 我们通过调用question_a()函数(您已经知道)开始执行,并使用if表达式检查点。 The for statements are used to make loops (repeating a piece of code X amount of times) for语句用于进行循环(重复一段代码X次)

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. 尝试先阅读Python上的一些基本语法,以便您可以轻松地处理if语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM