【问题标题】:Why can't I use str variables in input("text")?为什么我不能在 input("text") 中使用 str 变量?
【发布时间】:2018-07-30 20:45:36
【问题描述】:

尝试通过使用 str 输入变量将幸运轮计算器作为课堂任务。

例子:

int(input("In which day of",a(defiend as a month inputed before),"were you born?"))

【问题讨论】:

  • 你的问题太宽泛了。请提供一些(缩进的)代码。

标签: string python-3.x variables input


【解决方案1】:

你不能这样做。你会得到一个说明一切的错误,输入只需要一个(!)参数。这不是 print():

TypeError: input expected at most 1 arguments, got 3

这对您有帮助吗?我使用了 Python 3.7 中引入的新 f-Strings!

month = "april"
day = int(input(f"In which day of {month} were you born? "))
print(f"{day}. {month}")

如果您使用较早的 Python 版本,则必须使用 .format(...):

month = "april"
day = int(input("In which day of {} were you born? ".format(month)))
print("{}. {}".format(day, month))

快乐的蟒蛇。

【讨论】:

  • int(input("In which day of" + month + "were you born? ")
  • 请将此添加为第二个答案。也许 Tomer Bitran 想删除他的问题。这损害了他的名誉!
  • 我仍然不知道这是否是 OP 正在寻找的答案。无论如何,你已经给出了很好的例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
相关资源
最近更新 更多