2016-05-30 27 views
0

我想学习python,并且我遵循关于verson 3的视频指令并使用最新的Pycharm IDE。我的代码中出现错误,我找不到,初学者程序

我的屏幕看起来像指导员的屏幕,但我可以通过盯着它看太久。他的代码在我的崩溃时执行完美。我错过了什么?

错误消息:

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,构建于May 25,2016 JRE:1.8.0_77-b03 x86 JVM:Java HotSpot™服务器虚拟机,由 Oracle公司

现在,我只是盲目的或者是有可能在我的版本和教师版本之间有一个小更新已经发生了一个可能的IDE的问题,他正在教蟒蛇3.

非常感谢提前任何人都可以抛出的帮助。

+0

就像错误信息所说的那样,'input'只有一个参数。因此,将大部分内容放入'print'调用中,并将'':''作为'input'提示符。 –

+0

您可能会在'input()'中将'''用'连接运算符'+'混淆 - 将所有''改为'+',您应该没问题。 – Bassem

+0

'输入预期最多1个参数,得到5''你传递5个参数,你应该传递1.迈克尔 – njzk2

回答

2

在Python中,input运算符会接受一个输入(您希望显示的字符串)。同样在Python中,字符串连接使用+运算符完成。在你当前的操作中,你传递5个单独的字符串,而不是你想要使用的1个字符串。改变这一行代码:

balance = float(input("OK, "+ name +". Please enter the cost of the" + item + ": ")) 
0
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 
+0

OP显然使用了python 3,其中'input'用于获取一个字符串而不是'raw_input',而'print'函数需要像所有其他函数一样使用'()'括号 –

0

一点点改写可以澄清

input_message = "OK, {name}. Please enter the cost of the {item}: ".format(name=name, item=item) 
balance = float(input(input_message)) 

input参数应该只是一个字符串,我所要建造使用formathttps://docs.python.org/2/library/string.html#format-examples

你通过5个对象,说:

  • "OK, "
  • name
  • ". "Please enter the cost of the "
  • item
  • ": "

因此所述TypeError

要考虑到应验证实际的输入被转换成一个浮子,如果我输入“foobar”作为输入,则输入上面的行会给你一个ValueError,你可以自己检查。

0

尝试使用字符串格式运算符%s%是一个保留字符,可以直接放入输入字符串中。如果可能,跟在%后面的s将该变量格式化为一个字符串。如果你需要一个整数,只需使用%d。然后列出的变量出现的顺序由%

balance = float(input("OK %s. Please enter the cost of the %s: " %(name,item))) 

开头的字符串中你一定要小心,不要改变整数或除非你想这样的事情发生,我不建议这样做在漂浮成字符串一个输入语句。

+0

谢谢大家!用'+'代替','解决了这个问题。现在真正困扰我的是,这个错误如何影响了我正在观看的讲座! –

相关问题