Python通过if else和def添加

时间:2016-02-12 01:30:04

标签: python if-statement

所以我最初将这段代码写成一堆if / elif / else语句来获得积分,以便找出一个人是谁。问题是我无法弄清楚如何使else语句回到原始问题,因为该问题仅由variable = raw_input语句表示。我的想法是让很多朋友错综复杂。在使用else语句修复原始问题时,我搞砸了添加部分以确定答案。如何修复它实际计数呢?正如你所看到的,我对编码非常陌生。我很抱歉这个基本问题,因为我确信答案非常简单。我感谢任何帮助。

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 个答案:

答案 0 :(得分:1)

我希望你喜欢编程的世界:) 我已对代码进行了一些更改以使其正常工作,我会将它们评论出来。

Serial.println("Sending to Server: ");   
client.print("PUT /***/***/sensor/uod/1/A/1 HTTP/1.1\n");
Serial.print("PUT /***/***/sensor/uod/1/A/1 HTTP/1.1");                                       
client.print("Host: *********.koding.io\n");
client.print("Cache-Control: no-cache\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: 10\r\n\r\n");
client.print("status=1\r\n");

在每个函数(question_a(),question_b()..)中,对函数的调用应该在 else 语句中缩进,以便再次询问,以防答案不是 no

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"

在您要修改Tom,Chris或Jon变量的函数中,您需要通过使用行来指示Python

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

如果你想增加变量的分数,你可以这样做:

global Tom, Chris, Jon

因为如果你写了像

这样的东西
Tom = Tom + 1
Chris = Chris + 1
Jon = Jon + 1

你正在制作一个布尔表达式,它会给你真或假。

最后

Tom == Tom + 1

我们通过调用question_a()函数(如您所知)开始执行,并使用 if 表达式检查点。 for 语句用于创建循环(重复一段代码X次)

答案 1 :(得分:0)

这样做:

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

首先尝试阅读Python上的一些基本语法,以便您可以轻松处理if语句。