3
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.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
LORDNARWHAL1
  • 31
  • 1
  • 1
  • 2

1 Answers1

9

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
>>>
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172