所以我试图制作一些有趣的代码来测试我的技能,并且我试图让第一个if语句继续下一个if语句,即如果他们回答是的话。如果他们回答除了是之外的任何事情,则代码结束。我如何更改它,以便如果答案错误,代码实际上会结束?
print('Would you like to begin?')
answer = input()
if answer == 'yes':
print('What is your name?')
#continue to the next portion when if statement is true
else:
print('ok')
#break the code when else statement is true
name = input()
if name == 'Test':
print('Congratulations! You won!')
else:
print('Good try. The answer was Test.')
答案 0 :(得分:1)
将#break the code when else statement is true
替换为import sys; sys.exit(0)
。
如果他们回答'yes'
,则只会继续name = input()
。