我的程序只需问3个问题就能猜出用户的号码(从1到8)。它正确地打印了前两个问题,但是当我为第三个问题按enter键时,它只打印了最后一个输入。如何使所有输入(是或否)小写?
# Simple Expert System
#firstQuestion = prstr(firstQuestion.lower())
print("Think of a number between 1 and 8.")
firstQuestion = (raw_input("Is it an even number? "))
secondQuestion = "Is it less than or equal to 4? "
thirdQuestion = "Is it less than or equal to 3? "
fourthQuestion = "Is it less than 3? "
fifthQuestion = "Is it greater than 6? "
sixthQuestion = "Is it greater than 5? "
seventhQuestion = "Is it less than 2? "
if firstQuestion == "yes":
print(raw_input(secondQuestion))
elif firstQuestion == "no":
print(raw_input(thirdQuestion))
elif secondQuestion == "yes":
print(raw_input(fourthQuestion))
elif secondQuestion == "no":
print(raw_input(fifthQuestion))
elif thirdQuestion == "no":
print(raw_input(sixthQuestion))
elif thirdQuestion == "yes":
print(raw_input(seventhQuestion))
elif fourthQuestion == "yes":
print("Your number is 2")
elif fourthQuestion == "no":
print("Your number is 4")
elif fifthQuestion == "yes":
print("Your number is 8")
elif fifthQuestion == "no":
print("Your number is 6")
elif sixthQuestion == "yes":
print("Your number is 7")
elif sixthQuestion == "no":
print("Your number is 5")
elif seventhQuestion == "yes":
print("Your number is 1")
elif seventhQuestion == "no":
print("Your number is 3") 发布于 2016-10-03 14:46:05
首先,你要求第一个问题的投入。这会将答案放入firstQuestion变量中。然后你进入中频区。在这里,您向raw_input请求另一个问题,然后告诉程序打印该值。在这一点上,一个埃利夫的已经成功,而其他的被跳过。
为了达到预期的结果,您应该为应该问的每个新问题创建一个独立的if-组,或者创建一个while-循环。
例如:
# Simple Expert System
#firstQuestion = prstr(firstQuestion.lower())
print("Think of a number between 1 and 8.")
firstQuestion = (raw_input("Is it an even number? "))
secondQuestion = "Is it less than or equal to 4? "
thirdQuestion = "Is it less than or equal to 3? "
fourthQuestion = "Is it less than 3? "
fifthQuestion = "Is it greater than 6? "
sixthQuestion = "Is it greater than 5? "
seventhQuestion = "Is it less than 2? "
if firstQuestion == "yes":
secondQuestion = raw_input(secondQuestion)
elif firstQuestion == "no":
thirdQuestion = raw_input(thirdQuestion)
if secondQuestion == "yes":
fourthQuestion = raw_input(fourthQuestion)
elif secondQuestion == "no":
fifthQuestion = raw_input(fifthQuestion)
if thirdQuestion == "no":
sixthQuestion = raw_input(sixthQuestion)
elif thirdQuestion == "yes":
seventhQuestion = raw_input(seventhQuestion)
if fourthQuestion == "yes":
print("Your number is 2")
elif fourthQuestion == "no":
print("Your number is 4")
if fifthQuestion == "yes":
print("Your number is 8")
elif fifthQuestion == "no":
print("Your number is 6")
if sixthQuestion == "yes":
print("Your number is 7")
elif sixthQuestion == "no":
print("Your number is 5")
if seventhQuestion == "yes":
print("Your number is 1")
elif seventhQuestion == "no":
print("Your number is 3") 发布于 2016-10-03 14:52:42
考虑一下,对于较大的数字,您的程序没有很好的扩展性:如果您必须猜测一个介于1到1000之间的数字,那么您将不得不编写大量的代码。
相反,考虑循环遍历可能得到的所有范围:
lower_limit = 1
upper_limit = 100
while lower_limit < upper_limit:
middle = int(0.5 * (lower_limit + upper_limit))
check = raw_input("Larger than " + str(middle) + "? ")
if check.lower().startswith("y"): # Accept anything that starts with a Y as "yes"
lower_limit = middle + 1
else:
upper_limit = middle
print(lower_limit)发布于 2016-10-03 14:44:47
实际上,你的程序不能超越第二个问题,
if firstQuestion == "yes":
print(raw_input(secondQuestion))
elif firstQuestion == "no":
print(raw_input(thirdQuestion))无论我是否回答第一个问题,代码将在这两种情况之一,因此不能进入您的程序的其余部分。
您必须编写您的程序,考虑所有可能的场景,以及如何达到它们:
if firstQuestion == "yes":
#The user answered "yes" to the first question
if secondQuestion == "yes":
#The user answered "yes" to the first question and "yes" to the second
elif secondQuestion == "no":
#The user answered "yes" to the first question and "no" to the second
elif firstQuestion == "no":
#The user answered "no" to the first question
#etc...通过将此图表继续到所有级别,您将了解游戏的所有场景。
https://stackoverflow.com/questions/39834132
复制相似问题