【问题标题】:Respond to inputs in Python响应 Python 中的输入
【发布时间】:2021-10-05 12:57:43
【问题描述】:

我正在尝试制作一个使用 Python 学习英语语法的简单游戏。基本上,用户输入动词形式,Python 应该返回“正确”或“不正确”。我正在尝试使用一个函数来做到这一点:

def simple_past():
    question1 = input('I ____ to the park. Input: ').lower()  
    if input == 'went':
        print('correct')
    else:
        print('incorrect')

但是当我输入“went”时,它总是发回“不正确”。我的问题是双重的:(1)为什么即使我输入“went”,它也会返回“不正确”,(2)这是进行这种活动的最佳方式吗?干杯

【问题讨论】:

  • if question1 == 'went': 是你应该写的
  • input 只是一个内置的 Python 函数。在您的第二行中,您将它的返回值分配给变量 question1 并且您应该检查它是否是 == 'went'

标签: python function input


【解决方案1】:
def simple_past():
    question1 = input('I ____ to the park. Input: ').lower()  
    if question1 == 'went':
        print('correct')
    else:
        print('incorrect')

来自输入的值将存储在question1 变量中。所以你应该比较question1'went'

【讨论】:

    【解决方案2】:

    这就是我们应该为变量赋予有意义的名称的原因。

    def simple_past():
        input_value = input('I ____ to the park. Input: ').lower()  
        if input_value == 'went':
            print('correct')
        else:
            print('incorrect')
    

    【讨论】:

      猜你喜欢
      • 2022-10-02
      • 2021-09-03
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 2021-04-01
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多