2017-05-07 55 views
-4

我想創建一個「你最擅長哪個電視節目」。 但是,當它輸出哪個節目時,你應該將它們全部說出來。我該如何解決?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

您不需要在此處轉儲所有代碼。瞭解如何創建[mcve]。訓練自己以最少量的代碼在新腳本中重現問題。你會學到很多東西。 –

回答

-1

我想你可能會錯誤地使用or聲明。你的意思是q1.lower() == 'laugh' or '1'。這評估爲q1.lower() == 'laugh''1'。我相信Python中的一個字符串是「truthy」,因此它將所有這些語句評估爲真。

您將需要將您的if語句更改爲if q1.lower() == 'laugh' or q1.lower() == '1',或者完全消除數字響應。

+1

它的計算結果爲:'(q1.lower()==「笑」)或「1'' –

+0

@PeterWood謝謝,我已經編輯我的反應,以反映 – AetherUnbound

0

您肯定是在使用or聲明。任何非空字符串將被評估爲True。在評估q1.lower()多次的替代,你不妨檢查q1.lower()是在表達一個列表,即:

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

我減少if語句布爾表達式的數量並使其更容易爲你修改答案組合。

編輯:

您也可以使用套,這是更快的答案大組合:

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

爲什麼不設置,而不是一個列表? –

+0

@PeterWood和錢德勒帕克仍然需要投入的條件在無限循環在我的答案否則他會得到多個結果。 –

+0

你已經忘記了元組,不是嗎? –

0

你可以找出爲什麼你要在鏈接複製錯誤的結果: How do I test one variable against multiple values?

但是,更好的方式來做你想做的事情就是建立一棵樹。一個很好的方法,使一棵樹在Python是使用defaultdict

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' 

你也可以將所有輸入:

from collections import defaultdict 

def Tree(): 
    return defaultdict(Tree) 

然後,您可以與所有你想要的(例如)的值初始化它以小寫字母輸入。然後你不必再繼續做下去。此外,您的變量名可以更好地:

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

最終用途:

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

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

編輯:

你輸入的代碼是重複的,並要求被製作成一個功能:

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

用法:

villain = choose('Type of Villain?', 
       ['Supernatural', 'Humans', 'Social Pressure'], 
       'Death by: ') 
相關問題