我试图让用户输入 10 个名字并将它们放入一个列表中并删除该列表中的偶数单词

发布于 2025-01-19 08:57:57 字数 197 浏览 1 评论 0原文

name1 = str (input("Please Enter a Name:"))

lista = []
lista.append(name1)
if name1 % 2 == 0:
    lista.remove(name1)

这是我的代码搞乱的部分,它显示“TypeError:并非所有参数在字符串格式化期间都被转换”

name1 = str (input("Please Enter a Name:"))

lista = []
lista.append(name1)
if name1 % 2 == 0:
    lista.remove(name1)

This is the part where my code messes up it says " TypeError: not all arguments converted during string formatting"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

把时间冻结 2025-01-26 08:57:57
lista = []
for i in range(10):
    lista.append(str(input("Please Enter a Name:")))

for name in lista:
    if len(name) % 2 == 0:
        lista.remove(name)

# driver code to test
print(lista)

示例输入: ab cc d gg e ggg ff

输出: ['a', 'b', 'd', 'e', 'ggg']

但是,通过检查提供的输入的长度,这可以更加简化提供了它,并且仅添加奇数长度的单词。为您节省一个循环。这是代码:

lista = []
for i in range(10):
    word = str(input("Please Enter a Name:"))
    if not len(word) % 2 == 0:
        lista.append(word)

# driver code to test
print(lista)
lista = []
for i in range(10):
    lista.append(str(input("Please Enter a Name:")))

for name in lista:
    if len(name) % 2 == 0:
        lista.remove(name)

# driver code to test
print(lista)

Example input: a b cc d gg e ggg ff

Output: ['a', 'b', 'd', 'e', 'ggg']

However, this could be even more simplified, by checking the length of provided input as it's provided, and adding only odd-length words instead. Saves you one loop. Here's the code:

lista = []
for i in range(10):
    word = str(input("Please Enter a Name:"))
    if not len(word) % 2 == 0:
        lista.append(word)

# driver code to test
print(lista)
淡淡绿茶香 2025-01-26 08:57:57

这里的错误是 str上的操作员用于格式化;向操作员的参数(右侧的所有内容)需要与字符串中的格式化指令相对应。您正在尝试将其用作int模量运算符,因为它是str

如果您的目的是根据字符串的 length 奇数,甚至使用len()函数,请注意:请注意,请注意:请注意,请注意:请

odd_names = []
for _ in range(10):
    n = input("Please Enter a Name:")
    if len(n) % 2:
        odd_names.append(n)

注意您无需将input()的结果转换为str,并且您只需使用len(n)%2查看len(n)是否奇怪(因为1是true,而0为false)。

使用Python的当前版本合理的版本,您可以在一个简单的列表理解中完成这一切:

odd_names = [n for _ in range(10) if len(n := input("Please Enter a Name:")) % 2]

The error here is that the % operator on a str is for formatting; the arguments to the operator (everything to the right of it) need to correspond to formatting directives inside the string. You're trying to use it as the int modulus operator, which doesn't work because it's a str.

If your intent is to filter based on whether the length of the string is odd or even, use the len() function on the string, as others have suggested:

odd_names = []
for _ in range(10):
    n = input("Please Enter a Name:")
    if len(n) % 2:
        odd_names.append(n)

Note that you don't need to convert the result of input() to a str, and that you can simply use len(n) % 2 to see if len(n) is odd (because 1 is true and 0 is false).

With a reasonably current version of Python you can do this all in a single easy list comprehension:

odd_names = [n for _ in range(10) if len(n := input("Please Enter a Name:")) % 2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文