【问题标题】:How can I ask for a float input and an integer at the same time?如何同时要求浮点输入和整数?
【发布时间】:2015-10-22 19:08:37
【问题描述】:

我想要求受试者先输入一个浮点数(如 3.666),然后输入一个整数。 我所做的是:

x,y = input ( " Please enter two numbers"). split () 

然后转换

x,y =[ float (x), int (y)] 

它不起作用-有什么建议吗?

【问题讨论】:

  • 你应该使用 raw_input()。它们之间的区别在于“input()”将输入扫描为数字,而“raw_input()”将其扫描为字符串。
  • 扫描一个数字并用空格分割它是没有意义的。希望你能理解。
  • 下次发问题时,请在标签中添加语言名称:)
  • 我会的!非常感谢您的帮助!!!!!!
  • 如果 Laurie 使用的是 python3,输入就可以了

标签: python input integer


【解决方案1】:

您必须将它们扫描为字符串,用空格分隔作为分隔符,然后将它们类型转换为相应的类型。

s = raw_input("Please enter two numbers: ")
x,y = s.split(" ")
x = float(x)
y = int(y)

希望有帮助:)

【讨论】:

    猜你喜欢
    • 2019-09-17
    • 2021-01-04
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    相关资源
    最近更新 更多