【问题标题】:How to read user input?如何读取用户输入?
【发布时间】:2020-05-21 19:51:35
【问题描述】:

我正在编写一个程序,询问用户他们的感受,并根据用户输入的内容给出不同的响应。例如,如果你很开心或心情很好,它会在屏幕上打印“高兴听到”。但是,我不知道如何阅读用户输入。我应该尝试什么? (**里面的文字是我需要帮助的)。谢谢!


`name = input('what is your name?')
print('hello '+ name)
you = input('how are you doing today?')
**if you = happy, good, ok:
print('Glad to hear!')
else:
    print('I am sorry, I think I can cheer you up.')**
game = input('wanna play hangman?')    
print('yay! Ill start it up.')
print('hangman starting up...')`    

在这之后,一场刽子手游戏开始了。

【问题讨论】:

  • 看起来您确实知道如何读取用户输入,因为这段代码已经在使用 input() 函数。
  • 你想要这样的东西吗? if ["happy", "good", "ok"] in you: print("Glad to hear")
  • @RajeshM 你倒退了。 if you in ["happy", "good", "ok"]:

标签: python python-3.x if-statement user-input


【解决方案1】:

if you in ['good','great','awesome'] 是我猜的一种方式...

【讨论】:

    【解决方案2】:

    欢迎来到 StackOverflow!

    如果我理解正确,您要做的是字符串比较:

    name = input('what is your name?')
    print('hello '+ name)
    you = input('how are you doing today?')
    # ok, we need to compare the input stored in "you"
    # first way is to try expected things, and we can lower case it to facilitate.
    if you.lower() == 'good' or you.lower() == 'great':
        print('Glad to hear!')
    # ... rest of the code
    

    或者:

    if you.lower() in ['good', 'great', 'awesome']:
        print('Glad to hear!')
    

    【讨论】:

      【解决方案3】:

      我相信这就是你所需要的:

      name = input('what is your name?')
      print('hello '+ name)
      you = input('how are you doing today?')
      if you == 'happy' or you == 'good' or you == 'ok':
      print('Glad to hear!')
      else:
          print('I am sorry, I think I can cheer you up.')
      game = input('wanna play hangman?')    
      print('yay! Ill start it up.')
      print('hangman starting up...') 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 2011-11-08
        • 1970-01-01
        相关资源
        最近更新 更多