Python Personality Quiz,如何显示一个输出而不是所有输出

时间:2017-05-07 06:53:48

标签: python

我正在努力创造一个“你最擅长的电视节目。” 然而,当它输出显示你应该在 - 它说它们全部。我该如何解决这个问题?

我不在乎我是否花了很长时间来完成代码 - 我只是想知道如何解决一个答案。感谢。

def quiz():
print("Hello- what is your name?")
name = input("Name: ")
print("Hello " + str(name) + ", I will be asking you some questions today to find out what television show you'd be best in.")
print(" ")
print("Are you ready?")
print("Please answer with YES or NO: ")
response = input()
if response.lower() == 'yes':
    print(" ")
    print("Alrighty then.")
    print(" ")
    print("Do you prefer to: ")
    print(" ")
    print("Laugh")
    print("Think")
    print("Fight")
    print(" ")
    q1 = input("Choose carefully: ")
    print(" ")
    print("Type of Villain?")
    print(" ")
    print("Supernatural")
    print("Humans")
    print("Social Pressure")
    print(" ")
    q2 = input("Death by: ")
    print(" ")
    print("Genre: ")
    print(" ")
    print("Action")
    print("Comedy")
    print("Drama")
    print("Horror")
    print(" ")
    q3 = input("What will you pick: ")
    print(" ")
    print("You like to dress: ")
    print("Trendy")
    print("Casual")
    print("Eccentric")
    print("Elegantly")
    print("Whatever I can get my hands on")
    q4 = input("Fasionably...: ")
    print(" ")
    print("What would you use to defend yourself in a bad situation: ")
    print("My own intelligence")
    print("Bow and Arrows")
    print("My teeth")
    print("A sword or something sharp")
    print("Electronic Device")
    print("What bad situation...")
    print("A gun")
    q5 = input("Incidentally enough- pick your poison: ")
    print(" ")
    print("Which superower seems the best to you: ")
    print("Super Strength")
    print("Hyper-Observance")
    print("Advanced Intelligence")
    print("Invisibility")
    print("Teleportation")
    print("Telepathy")
    print("Immortality")
    q6 = input("Power: ")

    if q1.lower() == 'laugh' or '1':
        if q2.lower() == 'humans' or '2':
            if q3.lower() == 'action' or 'comedy' or '1' or '2':
                if q4.lower() == 'casual' or '2':
                    if q5.lower() == 'my own intelligence' or 'intelligence' or 'my' or 'own' or '1':
                        if q6.lower() == 'hyper-observance' or 'hyper' or 'observance' or '2':
                            print("Your television show is... Psych!")
    if q1.lower() == 'think':
        if q2.lower() == 'humans':
            if q3.lower() == 'drama' or '3'  or 'action' or '1':
                if q4.lower() == 'elegantly' or '4':
                    if q5.lower() == 'a sword or something sharp' or 'a' or 'sword' or 'or' or 'something' or 'sharp' or 'something sharp' or '4' or 'bow and arrows' or 'bow' or 'arrows' or '2':
                        if q6.lower() == 'advanced intelligenct' or 'intelligence' or '3':
                            print("Your television show is... Game of Thrones!")
    if q1.lower() == 'think' or '2' or 'fight' or '3':
        if q2.lower() == 'humans' or '2' or 'social pressure' or 'social' or 'pressure' or '3':
            if q3.lower() == 'comedy' or '2' or 'drama' or '3' and q4.lower() == 'trendy' or '1':
                if q5.lower() == 'my own intelligence' or 'my' or 'own' or 'intelligence' or '1' or 'electronic device' or 'electronic' or 'device' or '5' or 'what bad situation?' or 'what' or 'bad' or 'situation' or 'what bad situation' or '6':
                    if q6.lower() == 'advanced intelligence' or 'intelligence' or '3' or 'telepathy' or '6':
                        print("Your television show is... Gossip Girl!")
    if q1.lower() == 'think' or '2' or 'fight' or '3':
        if q2.lower() == 'humans' or '2' or 'supernatural' or '1':
            if q3.lower() == 'action' or 'drama' or '1' or '3':
                if q4.lower() == 'casual' or '2' or 'whatever i can get my hands on' or '5':
                    if q5.lower() == 'my own intelligence' or 'intelligence' or 'my' or 'own' or '1' or 'a gun':
                        if q6.lower() == 'super strength' or 'super' or 'strength' or '1' or 'invisibility' or '4':
                            print("Your television show is... The Walking Dead!")
    if q1.lower() == 'think' or '2' or 'fight' or '3':
        if q2.lower() == 'supernatural' or '1' or 'humans' or '2':
            if q3.lower() == 'action' or '1' or 'horror' or '4':
                if q4.lower() == 'whatever i can get my hands on' or '5':
                    if q5.lower() == 'my' or 'own' or 'intelligence' or 'my own intelligence' or '1':
                        if q6.lower() == 'teleportation' or '5':
                            print("Your television show is... Lost!")
    if q1.lower() == 'laugh' or '1':
        if q2.lower() == 'social pressure' or 'social' or 'pressure' or '3':
            if q3.lower() == 'comedy' or '2' or 'drama' or '3':
                if q4.lower() == 'trendy' or '1' or 'casual' or '2':
                    if q5.lower() == 'what bad situation' or 'what' or 'bad' or 'situation' or 'what bad situation?' or '6':
                        if q6.lower() == 'telepathy' or '6':
                            print("Your television show is... Friends!")
    if q1.lower() == 'think' or '2' or 'fight' or '3':
        if q2.lower() == 'humans' or '2':
            if q3.lower() == 'drama' or '3' or 'horror' or '4':
                if q4.lower() == 'casual' or '2':
                    if q5.lower() == 'my own intelligence' or 'intelligence' or '1' or 'a gun' or 'gun' or '7':
                        if q6.lower() == 'hyper observance' or 'hyper' or 'observance' or '2' or 'telepathy' or '6':
                            print("Your television show is... Criminal Minds!")
    if q1.lower() == 'think' or '2' or 'fight' or '3':
        if q2.lower() == 'supernatural' or '1':
            if q3.lower() == 'horror' or '4':
                if q4.lower == 'casual' or '2':
                    if q5.lower() == 'my own intelligence' or 'intelligence' or '1' or 'electronice device' or 'electronic' or 'device' or '5':
                        if q6.lower() == 'telekinesis' or '8':
                            print("Your television show is... Stranger Things")
    if q1.lower() == 'fight' or '3':
        if q2.lower() == 'supernatural' or '1' or 'humans' or '2':
            if q3.lower() == 'action' or '1' or 'drama' or '3':
                if q4.lower() == 'eccentric' or '3' or 'casual' or '2':
                    if q5.lower() == 'bow and arrows' or 'bow' or 'arrows' or '2':
                        if q6.lower() == 'super strength' or 'super' or 'strength' or '1':
                            print("Your television show is... Arrow!")
    if q1.lower() == 'fight' or '3':
        if q2.lower() == 'supernatural' or '1':
            if q3.lower() == 'drama' or '3' or 'horror' or '4':
                if q4.lower() == 'trendy' or '1':
                    if q5.lower() == 'my teeth' or 'teeth' or '3':
                        if q6.lower() == 'immortality' or '7':
                            print("Your television show is... Vampire Diaries!")

elif response.lower() == 'no':
    print("Goodbye then.")
else:
    print("Please come back when you're not such a child.")

1 个答案:

答案 0 :(得分:0)

您肯定使用or语句wront。任何非空字符串都将被评估为True。除了多次评估q1.lower()之外,您还可以检查q1.lower()是否在表达式列表中,即:

if q1.lower() in ['think', '2', 'fight', '3']:

我减少了if语句中布尔表达式的数量,使您更容易修改答案组合。

编辑:

您还可以使用集合,这对于大型答案组合来说更快:

if q1.lower() in {'think', '2', 'fight', '3'}: