python-3.7 - 如何修复 ' Not all arguments converted during string formatting? '

标签 python-3.7

我正在学习 python 。在做练习时,我遇到了一个问题

Python TypeError: not all arguments converted during string formatting



我试图引用和更改代码,但没有任何效果。
my_list=input(list)
for num in my_list:
    if(num%2==0):
        print(f"{num} is even number")
    else:
        print(f'{num} is odd number')

我预计输出应该是 num is even numbernum is odd number对于列表中的每个数字。不幸的是,我收到了上面的错误消息。

我怎样才能解决这个问题?

最佳答案

my_list=map(int, input().split())
for num in my_list:
    if(num%2==0):
        print(f"{num} is even number")
    else:
        print(f'{num} is odd number')

给出输入空格分隔的整数,例如:“1 2 3 4”,不带引号。输出应该符合预期。
该错误可能是由于您可能在列表中给出的输入格式,即“[1,2,3,4]”,它没有被python中的字符串格式隐式解码。要输入数字列表,您可能希望使用我在代码中提到的方式。

关于python-3.7 - 如何修复 ' Not all arguments converted during string formatting? ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353317/

相关文章:

python - 收到 pyserial 错误 "TypeError: ' >' not supported between instances of ' 字节' 和 'int'

python - 当单击而不是 shell 时,如何使按钮的值显示到 tkinter 上?

python - 如何将列表列表打印为合理的表格格式

python-3.7 - vtkCommonCorePython 在 Ubuntu 20 上的 python3-paraview 中丢失错误

python - 协程中的异步运行时错误 : no running event loop

python - 什么是数据类,它们与普通类有何不同?

python - 如何使用 Python 使用正则表达式从 Word 文档中提取问题

ubuntu - 如何让ttk使用OS主题?

python - 为什么 os.path.exists() 会阻止 Windows 命名管道连接?

python - Spyder 中的 "RuntimeError: asyncio.run() cannot be called from a running event loop"