2016-10-03 24 views
0

我的程序只需要询问3个问题就可以猜出用户的号码(从1到8)。它正确地打印了前两个问题,但是当我按下第三个问题的输入时,它只打印我所输入的最后一个输入。 如何使所有输入(是或否)小写?猜测数字从1到8

# 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") 
+1

'elif'的意思是“else if” – brianpck

+0

@brianpck和?为什么它不起作用?对不起,我很新的python –

回答

0

首先你要问第一个问题的输入。这将答案放入firstQuestion变量中。然后你进入if部分。在那里你问raw_input另一个问题,然后你告诉程序打印该值。在那一点上,elif's已经成功了,其他的都被跳过了。

你应该为所需的结果做什么是为每个应该问的新问题创建一个单独的if-group或创建一个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") 
+0

谢谢!我不知道为什么我没有想到这一点! :) –

+0

也你会如何使它无论输入的变化是什么,它会是小写?所以YES会最终成为“是”? –

+0

'if raw_input(question).lower()==“yes”'您也可以添加'.strip()'以避免尾随空白问题。 – brianpck

0

事实上你的程序不能走得更远比第二个问题,

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... 

,你有你游戏中的所有场景覆盖

+0

虽然这是对这个问题的答案,但它仍然不是一个好的解决方案。考虑一下:如果你必须编写一个必须找到1到1000之间的数字的程序呢?你会有一个嵌套'if's的船载吗?这就是发明的循环! – acdr

+0

当然,我试图通过使用嵌套条件来显示问题的图形结构,但它不是为任何大程序做的方式 – JMat

0

你能否提供输入你用吗?

尽管如此,我认为改善这个程序的结构可以帮助回答你的问题。

例如,注意你如何写:

firstQuestion = (raw_input("Is it an even number? ")) 
secondQuestion = "Is it less than or equal to 4? " 

既然你分配了“...的raw_input”到“firstQuestion”,“firstQuestion”不再持有该问题,但该问题的答案。这些线路之间缺乏一致性。这将更有意义做这样的事情:

firstQuestion = "Is it an even number? " 
secondQuestion = "Is it less than or equal to 4? " 
#put your other questions here 

firstAnswer = raw_input(firstQuestion) 
if firstAnswer == "yes": 
... 

通过在某种程度上改变你的程序,这样,逻辑和结果的行为会更加明朗。

6

考虑到你的程序根本不能很好地扩展数字:如果你必须猜测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) 
0

你有什么有没有真正从任何除第一个问题使用的输入。例如:

secondQuestion = "Is it less than or equal to 4? " 
# other stuff 
elif secondQuestion == "yes": # will never be true 

调用raw_input(secondQuestion)不会改变的secondQuestion的价值,它只是返回输入。你的情况意味着第二次打印它。

此外,您elif有什么恶意,它只会检查,直到第一个出来的真实的 - 所以,如果你回答'yes'第一个问题,它打印和第二个问题要求输入,并停止在那里。

东西会更喜欢你想要什么:

questions = [] 
questions.append("Is it an even number? ") 
questions.append("Is it less than or equal to 4? ") 
questions.append("Is it less than or equal to 3? ") 
questions.append("Is it less than 3? ") 
questions.append("Is it greater than 6? ") 
questions.append("Is it greater than 5? ") 
questions.append("Is it less than 2? ") 


answers = [raw_input(q) for q in questions] 
# Make sure to validate input 
while True: 
    if answers[0] == 'yes': 
     is_even = True 
     break 
    elif answers[0] == 'no': 
     is_even = False 
     break 
    else: 
     answers[0] = raw_input("Yes or no, please. ") 

# Now repeat that pattern with later questions 

while True: 
    if answers[1] == 'yes': 
     maximum = 4 
     break 
    elif answers[1] == 'no': 
     minimum = 5 
     break 
    else: 
     answers[1] = raw_input("Yes or no, please. ") 

# Continue collecting data like this for all questions, 
# Then use it at the end to get an answer 

肯定有一个更有效的方式来做到这一点 - 没有理由硬编码的所有问题,例如,它只是一个二进制搜索 - 但这比你写的更接近你。

+0

好主意,但不是很干。你会想要一个这样的功能。 – brianpck

+1

@brianpck恩,是的,我在底部说过。提问者决定对所有内容进行硬编码,这个问题实际上并不涉及他们对所有内容进行硬编码的事实,所以我试图让我的修复更像他们写的内容,这样工作代码和原始文件之间的关系就更容易被看到。我不想对人们可以做出的直觉有多大的假设,因为他们早在学习时就已经看起来像这样。 –