【发布时间】:2022-01-01 12:55:31
【问题描述】:
示例 = 如果该人输入 20,则说 20 不是正确答案 我正在尝试使用的当前代码是:
response = (input ('Type a number'))
if response == '5':
print('5 was the correct answer')
【问题讨论】:
示例 = 如果该人输入 20,则说 20 不是正确答案 我正在尝试使用的当前代码是:
response = (input ('Type a number'))
if response == '5':
print('5 was the correct answer')
【问题讨论】:
添加一个else 并使用 f-strings 将用户的输入插入到您的响应中:
answer = input('Type a number')
if answer == '5':
print(f'{answer} was the correct answer')
else:
print(f'{answer} was NOT the correct answer')
【讨论】:
您应该对正确答案使用“if”条件,对错误答案使用 else,同时使用适当的缩进:
correct = 5
response = input('Type a number: ')
if response == correct:
print('5 was the correct answer')
else:
print(response, "was NOT the correct answer")
【讨论】: