if / elif问题不起作用?

时间:2016-07-05 17:20:44

标签: python

我是Python新手并准备将其作为GCSE启动。我一直试图在学校做一个法国问题集的问题,对某些答案有不同的反应,但它一直没有奏效。语法错误' unindent与任何外部缩进级别都不匹配'在回答错误时(例如打印后('正确的国家/地区,错误的城市。再试一次。')回复后出现。有人可以帮忙吗?谢谢。

print('What is the capital of France?')
answer==input('Enter your answer:')
if answer == 'Paris':
    print('Well Done! That\'s Correct.')
 elif answer == 'Lyon':
     print('Right country, Wrong city. Try again.')
     input('Enter your answer:')
 elif answer == 'F':
    print('Terrible joke and the wrong answer. Try again.')
    input('Enter your answer:')
 else:
    print('Sorry, that\'s incorrect. Try again.')
    input('Enter your answer:')

4 个答案:

答案 0 :(得分:3)

您的缩进在if / elif / else块

中已关闭

如果您要分配变量,则应该是一个=而不是==

如果您想再次提示输入正确答案,则必须重新分配变量answeranswer = input(....)

此外,您可能希望运行while answer == 'Paris'或类似的东西,以便它连续提示,直到答案是您想要的值为

print('What is the capital of France?')
answer = input('Enter your answer:')
if answer == 'Paris':
    print('Well Done! That\'s Correct.')
elif answer == 'Lyon':
    print('Right country, Wrong city. Try again.')
    input('Enter your answer:')
elif answer == 'F':
    print('Terrible joke and the wrong answer. Try again.')
    input('Enter your answer:')
else:
    print('Sorry, that\'s incorrect. Try again.')
    input('Enter your answer:')

答案 1 :(得分:1)

除了之前的答案之外,我只想指出最好使用while循环并在while循环中将值分配给'answer'。这样,你也可以插入一个'break'语句,一旦你对'exit'这个单词进行数字处理或者提供正确的答案就会终止程序。这是代码:

print('What is the capital of France?')
while True:
    answer=input('Enter your answer, or type "exit" to close the program: ')  
    if answer=='exit':
        print('Program terminated')
        break 
    elif answer == 'Paris':
        print('Well Done! That\'s Correct.')
        break
    elif answer == 'Lyon':
         print('Right country, Wrong city. Try again.')
    elif answer == 'F':
        print('Terrible joke and the wrong answer. Try again.')
    else:
        print('Sorry, that\'s incorrect. Try again.')

答案 2 :(得分:0)

首先,当你想在变量中保存某些东西时,你只需使用一个= = = =它们

第二个你需要拥有所有elif,如果在同一个地方不使用制表符python对此非常敏感

对于答案,你需要做的循环可能一直重复,直到他们写出正确的问题

答案 3 :(得分:-1)

您不应该使用input(),而是使用raw_input()。