简体   繁体   中英

How do I get a certain response from a certain input

Example = If the person types in 20, say that 20 was NOT the correct answer My current code that I'm trying to work with is:

response = (input ('Type a number'))
if response == '5':
print('5 was the correct answer')

Add an else and use f-strings to insert the user's input into your response:

answer = input('Type a number')
if answer == '5':
    print(f'{answer} was the correct answer')
else:
    print(f'{answer} was NOT the correct answer')

You should use an "if" condition for your correct answer, and an else for incorrect ones, while also using proper indents:

correct = 5
response = input('Type a number: ')
if response == correct:
    print('5 was the correct answer')
else:
    print(response, "was NOT the correct answer")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM