not all arguments converted during string formatting.. NO % variables

31,793

The problem is that you take user input:

x = input()

Now x is a str. So, on this line:

    elif x % 2 == 0: #even

The % operator acts as a string interpolation operator.

>>> mystring = "Here goes a string: %s and here an int: %d" % ('FOO', 88)
>>> print(mystring)
Here goes a string: FOO and here an int: 88
>>>

However, the input you gave does not have a format specifier, thus:

>>> "a string with no format specifier..." % 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>

You need to convert your user input into an int for the % operator to perform the modulo operation.

x = int(input())

Now, it will do what you want:

>>> x = int(input("Gimme an int! "))
Gimme an int! 88
>>> x % 10
8
>>>
Share:
31,793
Admin
Author by

Admin

Updated on August 05, 2020

Comments

  • Admin
    Admin almost 4 years
    x = input()
    y = 1 
    print (x)
    while 1 == y:
    if x == 1:
        y == y + 1
    elif x % 2 == 0: #even
        x = x // 2
        print (x)
    else:
        x = 3 * x + 1
        print (x)
    

    If you know what the Collatz conjecture is, I'm trying to make a calculator for that. I want to have x as my input so I don't have to change x's number and save every time I want to try out a new number.

    I get below error

    TypeError: not all arguments converted during string formatting' at line 7.

    Please help a noobie out.