Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

TypeError: not all arguments converted during string formatting

What does this type error mean? I'm writing a program for the collatz conjecture:

x = input("pick a number") while x != 1: if x % 2 == 0: a = x/2 print(a) else: b = 3*x + 1 print(b)

it says the TypeError is in line three(if x%2 == 0:) Does that mean that "number" is not being passed into number/2 or 3*number +1? If so, what can I use to get an int as input?

1 Answer

Stuart Wright
Stuart Wright
41,103 Points

The input function always returns a string, even if the user inputs a number. You can't divide a string.

You might want to change your first line to:

x = int(input("pick a number"))

You will still get an error if the user enters anything that isn't an integer at the prompt, but as long as you only enter integers it will do what you want it to.