我在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!")
它讓我在沒有被定義的一些變量的錯誤,但我必須帶領用戶對某些問題,預測他們的對象,我不能定義它們。
始終把有問題的代碼和完整的錯誤消息。 – furas
順便說一句:你不必重複'如果'是'在'q1:'和類似的。 – furas