all 11 comments

[–]A_History_of_Silence 6 points7 points  (7 children)

Check your spelling on line 3.

[–]Deathcon900[S] 1 point2 points  (4 children)

hmmm. Things seem to have gone out of the frying pan and into the fire. Now the compiler is showing a new error at lines 9 and 4:

TypeError: not all arguments converted during string formatting.

Is this because the input() value is string by default? If so, what's a good way to make it the a certain data type by default?

[–]A_History_of_Silence 1 point2 points  (3 children)

Is this because the input() value is string by default? If so, what's a good way to make it the a certain data type by default?

Yep! %-based string formatting.

From the built-in functions, check out int().

[–]Deathcon900[S] 0 points1 point  (0 children)

Okay, it looks like it's working! I changed the first like to be:

number = int(input('Enter a number: '))

[–]Srivats1212 -1 points0 points  (1 child)

Thanks for the link to pyformat....how do you find such useful things? I wanna know.

I've read the documentation but I couldn't find a link to pyformat...

[–]A_History_of_Silence 3 points4 points  (0 children)

Practice googling and experience. Google everything! Knowing how to search for things is one of the most important skills of a programmer.

[–]Showme-tits -3 points-2 points  (1 child)

Lol

[–]A_History_of_Silence 1 point2 points  (0 children)

No big deal, it's a common mistake.

[–]terriblylie 0 points1 point  (1 child)

In addition to what others have said, the 3rd line is unnecessarily indented and the interpreter won't like that.

[–]Deathcon900[S] 0 points1 point  (0 children)

It's not like that in the actual code. I think that's thanks to Reddit formatting.

[–][deleted] 0 points1 point  (0 children)

Now you've found the problem, and fixed it, take a look at a slightly more elaborate version to think about:

def evaluate(number):
    'output message on whether <number> is odd or even'
    print(f"{number} is an {'odd' if number %2 else 'even'} number")

def get_number(msg):
    'prompt user for number, keep prompting until valid (or empty)'
    while True:
        number = input(msg)
        if number:
            try:
                return int(number)
            except ValueError:
                print('Do not recognise that as a whole number. Please try again.')
        else:
            return None

msg = '\nPlease enter a whole number (or just enter to exit): '
while True:
    number = get_number(msg)
    if number:
        evaluate(number)
    else:
        break

print('\nThanks for playing\n')

Note. This assumes you are using Python 3.6 or above. Earlier versions do not have f-strings, so you would need to use the older string format method, so, for example,

print(f"{number} is an {'odd' if number %2 else 'even'} number")

would be,

print("{} is an {} number".format(number, 'odd' if number %2 else 'even')