如果语句仅在运行所有三个语句时执行相应的print语句

时间:2014-09-29 12:27:24

标签: python python-3.x

我无法解释以下行为。

有人可以向我解释为什么,当我运行以下程序时,用户可以输入三个不正确的答案和应该阅读的“不正确”的打印声明。直到循环结束才打印?

def main():

UserName = input ('What is your name?')
UserAge = input ('What is your age?')


sum1 = input ('What is 3 x 3?')
sum2 = input ('What is 1 + 7?')
sum3 = input ('What is 9 - 8?')


if sum1 =='9':
    print('Correct1')

else:
    print ('Incorrect1')

if sum2 =='8':
    print('Correct2')
else:
    print ('Incorrect2')

if sum3 =='1':
    print('Correct3')
else:
    print ('Incorrect3')

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

我根据你的问题回答了这个问题,因为:“为什么在程序结束之前控制台打印错误/不正确?”

答案是:这是您定义程序的方式。 ie)在回复第一个问题之前,你要求对第二个问题提出意见。

见下面的修复:

def main():

 UserName = input ('What is your name?')
 UserAge = input ('What is your age?')


sum1 = input ('What is 3 x 3?')

if sum1 =='9':
print('Correct1')

else:
print ('Incorrect1')

sum2 = input ('What is 1 + 7?')

if sum2 =='8':
print('Correct2')
else:
print ('Incorrect2')

sum3 = input ('What is 9 - 8?')
if sum3 =='1':
print('Correct3')
else:
print ('Incorrect3')

if __name__ == '__main__':
main()