ValueError:以 10 为基数的 int() 的文字无效:“Blusson Hall”

发布于 2024-12-12 23:47:50 字数 836 浏览 4 评论 0原文

这是作业,但我不是在寻找讲义。错误消息还没有真正得到解释,所以我不知道如何解决这个问题或为什么会发生这种情况。我知道当我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扬花落满肩 2024-12-19 23:48:03

您无法将字符串转换为整数,这就是您在键入时尝试执行的操作:

n = int(s)

spaced 仅接受一个参数“s”,并且您传入一个字符串,然后尝试将其转换为整数。我认为你想要的可能是

n = len(s)

但实际上,你甚至不需要那个。由于字符串是可迭代的,因此您可以像这样循环遍历它:

for ch in s:
      ...do stuff here...

如果每个字符的 s 内的索引有帮助/更容易,则 enumerate() 将为您做到这一点:

   for idx, ch in enumerate(s):
       ...do stuff here...

只是让您知道,您实际上不需要 for 循环根本不。由于字符串是可迭代的,并且“join()”采用可迭代作为参数,因此您可以用以下代码替换几乎所有代码:

' '.join(s)

如果您以前没有使用过 Python,那么这看起来很奇怪。我创建了一个字符串“ ”,并且 join() 是所有字符串都可用的方法。 Join 接受一些可迭代对象(例如列表,甚至另一个字符串),并将作为连接对象的字符串放置在可迭代的每个元素之间。因此,在本例中,它在字符串 's' 的每个元素之间放置 ' '。

You can't convert a string to an integer, and that's what you try to do when you type:

n = int(s)

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

n = len(s)

But really, you don't even need that. Since strings are iterable, you can just loop over it like:

for ch in s:
      ...do stuff here...

And if the index within s for each char is helpful/easier, enumerate() will do that for you:

   for idx, ch in enumerate(s):
       ...do stuff here...

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:

' '.join(s)

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'.

倦话 2024-12-19 23:48:00

我想我看到了这个问题。

而不是

n = int(s)

尝试

n = len(s)

i think i see the issue.

instead of

n = int(s)

try

n = len(s)
耳根太软 2024-12-19 23:47:58

您的问题是您正在使用非数字字符串调用 spaced ,然后尝试将其转换为整数:

>>> int("Blusson Hall")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Blusson Hall'

如果您想要基于字符串长度的范围,您可以使用类似以下内容的内容:

for i in range(len(s)):

作为

>>> s = "Busson Hall"
>>> for i in range(len(s)):
...     print i
...
0
1
2
3
4
5
6
7
8
9
10

一些额外的帮助,您可以使用 s[i] 来获取 i 的第 i(当然,零是第一个)字符代码>s。此外,您可能希望从一个空字符串开始,然后向其附加单个字符(来自原始字符串和您想要添加的任何空格),以在返回之前逐渐构建它。

例如,这个片段复制了每个字符,它们之间用冒号隔开:

>>> s = "paxdiablo"
>>> s2 = ""
>>> for i in range(len(s)):
...     s2 = "%s%s:%s:" % (s2, s[i], s[i])
...
>>> print s2
p:p:a:a:x:x:d:d:i:i:a:a:b:b:l:l:o:o:

缺少为您编写代码(明智地,您决定不要求),这可能是我可以提供的所有帮助(尽管随意询问您的任何问题)想要,我会提供进一步的建议)。

Your problem is that you are calling spaced with a non-numeric string and then trying to convert that to an integer:

>>> int("Blusson Hall")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Blusson Hall'

If you want a range based on the length of the string, you can use something like:

for i in range(len(s)):

as in:

>>> s = "Busson Hall"
>>> for i in range(len(s)):
...     print i
...
0
1
2
3
4
5
6
7
8
9
10

And, as some extra assistance, you would use s[i] to get the i'th (zero being the first one, of course) character of s. 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:

>>> s = "paxdiablo"
>>> s2 = ""
>>> for i in range(len(s)):
...     s2 = "%s%s:%s:" % (s2, s[i], s[i])
...
>>> print s2
p:p:a:a:x:x:d:d:i:i:a:a:b:b:l:l:o:o:

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文