-1

我对此非常陌生,但我仍然不明白为什么这不起作用:

    print("Multiple Finder 2.0.1")
    print("MF2.0.1 will find the multiples of any number between point A and point B")
    multiple = input("Find the multiples of what number: ")
    startPoint = input("Enter Point A: ")
    endPoint = input("Enter Point B: ")
    x = startPoint
    while x <= endPoint:
    if x % str(multiple) == 0:
    print(x)
    x = x + 1

我得到这个错误

    Traceback (most recent call last):
    File "C:/Users/---", line 8, in <module>
    if x % str(multiple) == 0:
    TypeError: not all arguments converted during string formatting
4

1 回答 1

2

好吧, % 在 python 中可以做两件事。对于数字,它是模运算。对于字符串,它的格式操作。由于 x 和 str(...) 都是字符串,python 正在尝试执行后一个广告,然后将其与一个数字进行比较。我想你想要的是:

multiple = input("Find the multiples of what number: ")
startPoint = int(input("Enter Point A: ")) #use int() to get numbers from strings
endPoint = int(input("Enter Point B: ")) 
x = startPoint
while x <= endPoint:
    if x % multiple == 0: #modulo two integers
        print(x)
    x = x + 1
于 2013-11-06T06:01:13.027 回答