【问题标题】:How do I get a certain response from a certain input如何从某个输入中获得某个响应
【发布时间】:2022-01-01 12:55:31
【问题描述】:

示例 = 如果该人输入 20,则说 20 不是正确答案 我正在尝试使用的当前代码是:

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

【问题讨论】:

    标签: python input output


    【解决方案1】:

    添加一个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')
    

    【讨论】:

      【解决方案2】:

      您应该对正确答案使用“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")
      

      【讨论】:

        猜你喜欢
        • 2019-12-03
        • 2021-02-03
        • 1970-01-01
        • 2020-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-04
        • 1970-01-01
        相关资源
        最近更新 更多