首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >初学者程序在我的代码中找不到的错误

初学者程序在我的代码中找不到的错误
EN

Stack Overflow用户
提问于 2016-05-30 21:52:59
回答 4查看 110关注 0票数 0

我正在尝试学习python,并使用最新的Pycharm来学习第3节的视频说明。

我的屏幕看起来像指导员的屏幕,但我可以有隧道的视觉,因为盯着它太长时间。当我的代码崩溃时,他的代码执行得很好。我遗漏了什么?

错误消息:

代码语言:javascript
复制
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行:

代码语言:javascript
复制
# 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。

事先非常感谢任何人都能提供的帮助。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-05-30 22:11:34

在Python中,input操作符接受单个输入(要显示的字符串)。在Python中,字符串连接也是通过+操作符完成的。在当前操作中,您要传递5个单独的字符串,而不是要使用的1。将该代码行更改为:

代码语言:javascript
复制
balance = float(input("OK, "+ name +". Please enter the cost of the" + item + ": "))
票数 2
EN

Stack Overflow用户

发布于 2016-05-30 22:11:34

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-05-30 22:17:08

稍微重写一下就能弄清楚

代码语言:javascript
复制
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,您可以自己检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37534045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档