-4

I am trying to create a "Which television show would you be best in." However, when it outputs which show you should be in- it says them all. How do I fix this?

I don't care if I took a long route to complete the code- I just want to know how to fix to say one answer. Thanks.

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
    You didn't need to dump all your code on here. See how to create a [mcve]. Discipline yourself to reproduce the problem in a new script, with a minimal amount of code. You will learn a lot. – Peter Wood May 07 '17 at 07:12

3 Answers3

0

You are definitely using the or statement wront. Any non-empty string will be evaluated as True. An alternative to evaluating q1.lower() multiple times, you might as well check if q1.lower() is in a list of expression, i.e.:

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

I reduces the number of boolean expression in an if-statement and makes it easier for you to modify answer combinations.

EDIT:

You can also use sets, which is faster for large answer combinations:

if q1.lower() in {'think', '2', 'fight', '3'}:
  • Why not a set instead of a list? – Peter Wood May 07 '17 at 07:14
  • @PeterWood And Chandler Parker still need to put conditions in infinite loop as in my answer else he will get more than one result. – R. Gadeev May 07 '17 at 07:19
  • You have forgotten about tuples, didn't you? – R. Gadeev May 07 '17 at 07:21
  • @R.Gadeev Sure, tuples will do the job as well :-D - not going to edit it, though. However, the infinite loop is definitely not required. If your combination of answers is caught by one of the if-cycles, it will give you only one tv show as result – Christoph Engwer May 07 '17 at 07:27
0

You can find out why you're getting the wrong results in the linked duplicate: How do I test one variable against multiple values?.

However, a better way to do what you're trying to do is to build a tree. A nice way to make a tree in Python is to use defaultdict:

from collections import defaultdict

def Tree():
    return defaultdict(Tree)

You can then initialise it with all the values you want (for example):

shows = Tree()

shows['laugh']['supernatural']['action']['trendy'] = 'Lost'
shows['laugh']['supernatural']['horror']['elegantly'] = 'Open All Hours'
shows['think']['humans']['comedy']['casual'] = 'Game of Thrones'
shows['think']['humans']['action']['whatever i can get my hands on'] = 'Bod'
shows['fight']['social pressure']['drama']['eccentric'] = 'Friends'

You could also convert all input to lower case, when it's input. Then you don't have to keep doing it later. Also your variable names could be better:

preference = input("Choose carefully: ").lower()
villain = input("Death by: ").lower()
genre = input("What will you pick: ").lower()
dress = input("Fasionably...: ").lower()

Final usage:

show = shows[preference][villain][genre][dress]

if show:
    print('Your television show is... {}!'.format(show))
else:
    print("I haven't considered that combination")

edit:

Your input code is repetitive and is asking to be made into a function:

def choose(category, choices, prompt):
    print(category)
    print(" ")
    for choice in choices:
        print(choice)
    print(" ")
    return input(prompt).lower()

Usage:

villain = choose('Type of Villain?',
                 ['Supernatural', 'Humans', 'Social Pressure'],
                 'Death by: ')
Community
  • 1
  • 1
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
-1

I think you might be using the or statement incorrectly. You're saying q1.lower() == 'laugh' or '1'. This evaluates to q1.lower() == 'laugh' or '1'. I believe a string in python is "truthy", so it evaluates all those statements as true.

You will need to change your if statements to be if q1.lower() == 'laugh' or q1.lower() == '1', or do away with numeral responses entirely.

AetherUnbound
  • 1,714
  • 11
  • 10