【问题标题】:Python: asking user for input with prior input in the prompt? [duplicate]Python:要求用户在提示中预先输入输入? [复制]
【发布时间】:2018-03-24 20:56:24
【问题描述】:

我正在使用 Python,我想要求用户输入以定义变量,然后使用他们在第一个问题中作为第二个问题的一部分输入的内容来要求另一个输入。

这可能会令人困惑。我的意思是这样的:

hometeam = str(input("Home team?"))
ppg = input(hometeam "Average Points per Game?")
print(ppg)

例如,如果用户为主队输入“Cowboys”,我希望程序询问“Cowboys Average Points per Game?”第二个问题。

我收到一个语法错误,胡萝卜指向最后一个引号。

【问题讨论】:

  • 副本应该提供足够多的示例如何在 Python 中正确插入字符串。

标签: python input user-input


【解决方案1】:

您将该输入保存到变量“hometeam”中,因此您可以使用“+”执行类似的操作:

ppg = input(hometeam + " Average Points per Game?")

【讨论】:

    【解决方案2】:
    hometeam = str(input("Home team?"))
    ppg = input("{0} Average Points per Game?".format(hometeam))
    print(ppg)
    

    【讨论】:

    • 你可能想要一个空格:" Average Points per Game?"
    • 谢谢,更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多