【问题标题】:Assigning text to specific input variables in python在python中将文本分配给特定的输入变量
【发布时间】:2015-07-15 06:17:44
【问题描述】:
print "This is a dating message that I am going to test, I want the user to be able to input their own answers."
print "I'd like for this message to work correctly, this is a dating message for Python"

print "Hi there, I think you're very cute and that's why I'm sending you this message"
print "I'm going to prompt you to see if you'd like to go on a date with me, type 'ok' to continue"

raw_input() 
print "Type 'y' if you'd like to go out, or 'n' if you're not interested."

raw_input(y) = "Hooray I'm so happy to hear that! You should text me at 888-888-8888"
raw_input(n) = "Awww darnit, well best of luck to you!"

我对编程很陌生,实际上我现在正在上大学,但我只是好奇我在尝试将特定文本(例如输入)分配给 python 中的输入变量时遇到了麻烦,谁能告诉我我做错了什么?

【问题讨论】:

标签: python variables text input raw-input


【解决方案1】:

raw_input 是一个返回用户答案的​​函数。

yesno = raw_input("Type 'y' if you'd like to go out, or 'n' if you're not interested.")

if yesno == 'y':
    print "Hooray I'm so happy to hear that! You should text me at 888-888-8888" 
else:
    print "Awww darnit, well best of luck to you!"

【讨论】:

  • 好吧,我想我需要像这样开始跳出框框思考,我觉得我试图让事情变得过于复杂,哈哈。 TY 的帮助。
【解决方案2】:

如果我能正确理解,您希望将 raw_input 分配给某个变量。这将是解决它的方法

age =  raw_input("Enter your age: ")
print age

【讨论】:

    【解决方案3】:

    在尝试学习编程方面做得很好。你想做什么?

    您可以在控制台中使用此打印文本:

    print "Hello World"
    

    您可以使用它来提示用户并保存输入的内容:

    something = raw_input("Just type something? ")
    

    之后,您将在 /something/ 变量中输入用户输入的字符串,您可以像这样打印出来:

    print something
    

    这有帮助吗?

    【讨论】:

      【解决方案4】:

      您可以像这样为字符串对象分配名称:

      var = "This is a text string"
      

      现在每当我们使用var 时,它指的是“这是一个文本字符串”。

      print var
      This is a text string
      

      函数(只是命名的代码块)返回一个对象。它可以是任何 Python 对象,包括文本字符串。然后我们可以在赋值中使用该函数调用:

      reply = raw_input("Please type something: ")
      

      raw_input 返回用户输入的任何内容。现在我们可以使用名称reply 来引用该字符串对象。

      当然我们可以结合这些:

      prompt = "Please type something: " 
      reply = raw_input(prompt)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多