ValueError:以 10 为基数的 int() 的文字无效:“Blusson Hall”
这是作业,但我不是在寻找讲义。错误消息还没有真正得到解释,所以我不知道如何解决这个问题或为什么会发生这种情况。我知道当我尝试将 s 转换为整数时会发生这种情况,但如果不这样做,我会收到不同的错误,所以我有点迷失...... 我也尝试过查看其他一些有类似问题的帖子,但我对 Python 很陌生,我无法理解其中的解释。
我认为这是一个非常简单的函数。 我尝试将其转换为整数,以便可以对其执行范围函数,但这似乎不起作用。该程序应该首先在“Blusson Hall”中的字母之间添加一个空格,如果已经有一个空格,则添加一个额外的空格,最后在最终产品周围打印该设计。感谢您的任何帮助。
def spaced(s):
n = int (s)
for [i] in range (n):
if [i] != " ":
n == n+ [i] + " "
if [i] == " ":
n == n+ [i] + " "
print "-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-"
print ". ."
print "- " + str (n)+ " -"
print ". ."
print "-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-"
#- you write (5 marks) -#
###################
#- Tester's code -#
###################
spaced("Blusson Hall")
This is homework but I'm not looking for a handout. Error messages haven't really been explained yet so I don't know how to fix this or why it's happening. I know it happens when I try to make s into an integer but I get a different error if I don't so I'm a little lost...
I've also tried looking at some of the other posts with similar problems but I'm very new to Python and I can't follow the explanations.
It's a pretty straightforward function, I think.
I've tried converting it to an integer to I can perform the range function on it but that doesn't seem to be working. The program is supposed to first put a space between the letters in "Blusson Hall" and add an additional space if there is already one there and finally print that design around the final product. Thanks for any help.
def spaced(s):
n = int (s)
for [i] in range (n):
if [i] != " ":
n == n+ [i] + " "
if [i] == " ":
n == n+ [i] + " "
print "-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-"
print ". ."
print "- " + str (n)+ " -"
print ". ."
print "-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-"
#- you write (5 marks) -#
###################
#- Tester's code -#
###################
spaced("Blusson Hall")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法将字符串转换为整数,这就是您在键入时尝试执行的操作:
spaced 仅接受一个参数“s”,并且您传入一个字符串,然后尝试将其转换为整数。我认为你想要的可能是
但实际上,你甚至不需要那个。由于字符串是可迭代的,因此您可以像这样循环遍历它:
如果每个字符的 s 内的索引有帮助/更容易,则 enumerate() 将为您做到这一点:
只是让您知道,您实际上不需要 for 循环根本不。由于字符串是可迭代的,并且“join()”采用可迭代作为参数,因此您可以用以下代码替换几乎所有代码:
如果您以前没有使用过 Python,那么这看起来很奇怪。我创建了一个字符串“ ”,并且 join() 是所有字符串都可用的方法。 Join 接受一些可迭代对象(例如列表,甚至另一个字符串),并将作为连接对象的字符串放置在可迭代的每个元素之间。因此,在本例中,它在字符串 's' 的每个元素之间放置 ' '。
You can't convert a string to an integer, and that's what you try to do when you type:
spaced only takes one argument, 's', and you pass in a string, then try to convert it to an integer. I think what you want is probably
But really, you don't even need that. Since strings are iterable, you can just loop over it like:
And if the index within s for each char is helpful/easier, enumerate() will do that for you:
Just so you know, you don't actually need a for loop at all. Since strings are iterable and 'join()' takes an iterable as an argument, you can replace almost all of your code with this:
That looks odd if you haven't done much with Python before. I've created a string ' ', and join() is a method that all strings have available. Join takes some iterable object (like a list, or even another string) and puts the string that is the object of the join between each element of the iterable. So, in this case, it puts ' ' between each element of the string 's'.
我想我看到了这个问题。
而不是
尝试
i think i see the issue.
instead of
try
您的问题是您正在使用非数字字符串调用
spaced
,然后尝试将其转换为整数:如果您想要基于字符串长度的范围,您可以使用类似以下内容的内容:
作为
一些额外的帮助,您可以使用
s[i]
来获取i
的第i
(当然,零是第一个)字符代码>s。此外,您可能希望从一个空字符串开始,然后向其附加单个字符(来自原始字符串和您想要添加的任何空格),以在返回之前逐渐构建它。例如,这个片段复制了每个字符,它们之间用冒号隔开:
缺少为您编写代码(明智地,您决定不要求),这可能是我可以提供的所有帮助(尽管随意询问您的任何问题)想要,我会提供进一步的建议)。
Your problem is that you are calling
spaced
with a non-numeric string and then trying to convert that to an integer:If you want a range based on the length of the string, you can use something like:
as in:
And, as some extra assistance, you would use
s[i]
to get thei
'th (zero being the first one, of course) character ofs
. In addition, you probably want to start with an empty string and then append individual characters to it (from the original string and whatever spaces you want added) to gradually build it up before returning it.For example, this snippet duplicates every character with a colon between them:
Short of writing the code for you (which, intelligently, you decided against asking for), that's probably all the help I can give (though feel free to ask any questions you want and I'll offer further advice).