我正在尝试学习python,并使用最新的Pycharm来学习第3节的视频说明。
我的屏幕看起来像指导员的屏幕,但我可以有隧道的视觉,因为盯着它太长时间。当我的代码崩溃时,他的代码执行得很好。我遗漏了什么?
错误消息:
line 6, in <module>
balance = float(input("OK, ", name, ". Please enter the cost of the ", item, ": "))
TypeError: input expected at most 1 arguments, got 5程序的第一部分至第6行:
# Get information from user
print("I'll help you determine how long you will need to save.")
name = input("What is your name? ")
item = input("What is it that you are saving up for? ")
balance = float(input("OK, ", name, ". Please enter the cost of the ", item, ": "))pycharm版本是:
PyCharm社区版2016.1.4构建#PC-145.1504,构建于2016年5月25日,JRE: 1.8.0_77-b03 x86 JVM: HotSpot(TM)服务器VM
现在,我是瞎了还是有可能在我的版本和老师的版本之间的一个小更新中发生了一个ide问题,他在教python 3。
事先非常感谢任何人都能提供的帮助。
发布于 2016-05-30 22:11:34
在Python中,input操作符接受单个输入(要显示的字符串)。在Python中,字符串连接也是通过+操作符完成的。在当前操作中,您要传递5个单独的字符串,而不是要使用的1。将该代码行更改为:
balance = float(input("OK, "+ name +". Please enter the cost of the" + item + ": "))发布于 2016-05-30 22:11:34
print ("I'll help you determine how long you will need to save.")
name = raw_input("What is your name? ")
item = raw_input("What is it that you are saving up for? ")
balance = float(raw_input("OK, "+ name +". Please enter the cost of the "+ item +": "))
print name
print item
print balance发布于 2016-05-30 22:17:08
稍微重写一下就能弄清楚
input_message = "OK, {name}. Please enter the cost of the {item}: ".format(name=name, item=item)
balance = float(input(input_message))input的参数应该只是一个字符串,我使用format https://docs.python.org/2/library/string.html#format-examples构建该字符串。
您正在传递5个对象,例如:
"OK, "name". "Please enter the cost of the "item": "因此,TypeError
考虑到您应该验证要转换为浮动的实际输入,如果我输入"foobar“作为输入,上面的行将给出一个ValueError,您可以自己检查。
https://stackoverflow.com/questions/37534045
复制相似问题