【问题标题】:TypeError : not all arguments converted during string formatting python [duplicate]TypeError:在字符串格式化python期间并非所有参数都转换了[重复]
【发布时间】:2022-07-24 05:24:21
【问题描述】:

程序应该打印被分成三份的条目,没有余数。问题是底部的“TypeError:不是所有参数在字符串格式化python期间转换”

numbers = [ ]
while True:
    inputNumber = (input("Enter a number if you want to terminate this, please tap on 'q' : "))
    if inputNumber == "q":
        break    
    numbers.append(inputNumber)
    


sum = 0
for i in numbers:
    sum+=int(i)

print("Sum of the inputs : ", sum)

#unexecutable lines  
for i in numbers:
    if(i%3 == 0):
        print(i)

【问题讨论】:

  • i%3 的位置,i 是字符串,而不是数字。

标签: python


【解决方案1】:

变量i是字符串格式。

因此在执行计算之前将其类型转换为int

#unexecutable lines  
for i in numbers:
    if(int(i)%3 == 0):
        print(i)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 2015-01-21
    • 2021-08-09
    • 2021-08-19
    • 2015-10-28
    • 2013-10-21
    相关资源
    最近更新 更多