2017-01-27 118 views
-2

我在python初學者和有課堂作業創建類似於20-問題遊戲的腳本。我就這樣開始了至今:競猜對象的遊戲

# 20-Questions 
#first question 
q1= raw_input("Is it on the floor") 
#continuation for first quesion "yes" answers 
if 'yes' in q1: 
    print("It is on floor") 
if 'yes' in q1: 
    q2a =raw_input("Is it smooth") 
#continuation for first quesion "no" answers 
if 'no' in q1: 
    print("It is not on the floor") 
if 'no' in q1: 
    q2b=raw_input("Is it on the wall?") 
#continuation for second quesion "yes yes" answers 
if 'yes' in q2a: 
    print("It is smooth") 
if 'yes' in q2a: 
    q3a=raw_input("Is it floor?") 
#continuation for second quesion "yes no" answers 
if 'no' in q2a: 
    print("It is not smooth") 
if 'no' in q2a: 
    q3b=raw_input("Is it electronic?") 
#continuation for second quesion "no yes" answers 
if 'yes' in q2b: 
    print("It is on wall") 
if 'yes' in q2b: 
    q3c=raw_input("Is it a poster?") 
#continuation for second quesion "no no" answers 
if 'no' in q2b: 
    print("It is not on wall") 
if 'no' in q2b: 
    q3d=raw_input("Is it on the ceiling?") 
#continuation for third question "yes yes yes" 
if 'yes' in q3a: 
    print("It is floor, floor is good") 
#continuation for third question "yes yes no" 
if 'no' in q3a: 
    print("It must be desk,then...") 
#continuation for third question "yes no yes" 
if 'yes' in q3b: 
    print("It is computer, yes?, everthing electronic is computer") 
#continuation for third question "yes no no" 
if 'no' in q3b: 
    print("I dont know, lot of non-smooth and non-electronic things, not have enough time to code in") 
#continuation for third question "no yes yes" 
if 'yes' in q3c: 
    q3c=raw_input("Poster show great many things, so guess is good right?") 
#continuation for third question "no yes no" 
if 'no' in q3c: 
    q3c=raw_input("If poster not on wall, what is? Nail, cabniet, hole?; they not matter, poster matters") 
#continuation for third question "no no yes" 
if 'yes' in q3d: 
    q3d=raw_input("If on ceiling, then it must be light!") 
#continuation for third question "no no no" 
if 'no' in q3d: 
    q3d=raw_input("So it is not on celing, wall, or celing, where is it?; Your item must be invisible...") 
else: 
    raw_input("you silly goose!, it is yes or no question!") 

它讓我在沒有被定義的一些變量的錯誤,但我必須帶領用戶對某些問題,預測他們的對象,我不能定義它們。

+3

始終把有問題的代碼和完整的錯誤消息。 – furas

+0

順便說一句:你不必重複'如果'是'在'q1:'和類似的。 – furas

回答

0

你遇到麻煩的原因是,當你只問和設置與q2a相對應的問題/變量。變量q2b仍被檢查爲是或否。結果是你要求python檢查q2b是真還是假,即使這個人只有被問及q2a的問題。因此變量沒有設置,python給你一個錯誤。

解決方案: 您的代碼的組織有點混亂。 重新思考你組織這種方式的方式,以確保你不檢查未設置的變量。

+0

感謝您的快速回答,那麼我需要以只在需要時才設置變量的方式放置我的代碼? –

-1

創建一個函數,然後列出所有的問題在一個陣列

def twenty_questions(): 

**questions_array** = ["is it on the floor?","is it smooth?", " is it on the wall?"] 

    # while loop for conditions 
    while usr_guess < 21: # max of 20 questions 

     # each guess is incremented by 1 
     guesses_taken = guesses_taken + 1 

     # print out question from the array & ask user questions 
     usr_guess = input(" > ",questions_array[0]) 

     # yes or no to each question from the array 
     # repeat If-Else but change array value to fit question 

     if usr_guess ==("yes"): 
      print(questions_array[0]," = yes") 
     if usr_guess ==("no"): 
      print(questions_array[0]," = no") 
     else: 
      print(" yes or no questions") 
+0

嗨@New_usr,歡迎來到SO。請改善您的格式,難以理解您的答案。 –

+0

@AlejandroMontilla我編輯過,如果還不清楚,請告訴我一些具體細節,謝謝。 –