Question

I'm trying to write a program that asks the user for 4 integers and prints the largest odd number that was entered. Here is the code:

a = raw_input("Enter an int: ")
b = raw_input("Enter an int: ")
c = raw_input("Enter an int: ")
d = raw_input("Enter an int: ")

numbers = [a, b, c, d]
odd_numbers = []
print numbers
for i in numbers:
    if i%2!=0:
        odd_numbers.append(i)
    else:
        print "This is not an odd number."


for nums in odd_numbers:
    max_num = max(odd_numbers)
    print max_num

And here is the error that I'm receiving:

line 10, in <module>
  if i%2!=0:
TypeError: not all arguments converted during string formatting

What am I doing wrong ?

Was it helpful?

Solution 2

Because You are input is string convert it into int

>>> a =raw_input("Enter an int: ")
Enter an int: 10
>>> type(a)
<type 'str'> 

Try This :

a =int(raw_input("Enter an int: "))
b = int(raw_input("Enter an int: "))
c = int(raw_input("Enter an int: "))
d = int(raw_input("Enter an int: "))

OR

for i in numbers:
    if int(i)%2!=0:
             odd_numbers.append(i)

Your Output Should be look like this :

>>> 
Enter an int: 10
Enter an int: 20
Enter an int: 20
Enter an int: 50
[10, 20, 20, 50]
This is not an odd number.
This is not an odd number.
This is not an odd number.
This is not an odd number.

OTHER TIPS

raw_input() returns a string. As a result, numbers list becomes a list of strings. % operation behavior depends on the variable type, in case of string it is a string formatting operation:

>>> s = "3"
>>> s % 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

In case of int, it gives you a division remainder:

>>> n = 3
>>> n % 2
1

You need to convert all the inputs to int:

a = int(raw_input("Enter an int: "))
b = int(raw_input("Enter an int: "))
c = int(raw_input("Enter an int: "))
d = int(raw_input("Enter an int: "))

To avoid having a redundant code, you can simplify filling the numbers list using list comprehension:

numbers = [int(raw_input("Enter an int: ")) for _ in xrange(4)]

raw_input will return you a string. So, each element in numbers are string like

numbers = ["1", "2", "3", "4"]

When you try i%2, python evaluates % as the string formatting operator but could not find any place-holder in the string for formatting and raise error. So you must parse your input to int

a = int(raw_input("Enter an int: "))

Or you can use input, which will evaluate your input to proper type (int in your case)

a = input("Enter an int: ")

But using input is not recommended if you are not experienced with it and eval as it states in the docs:

Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users.

You input strings but you need to do calculations with ints. I you do print type(a) for instance you will see that you actually got a string as input. The way to parse it to an int is to use the built in function int().

a = raw_input("Enter an int: ")
b = raw_input("Enter an int: ")
c = raw_input("Enter an int: ")
d = raw_input("Enter an int: ")

numbers = [a, b, c, d]
odd_numbers = []
print numbers
for i in numbers:
    value = int(i)
    if value%2!=0:
        odd_numbers.append(value)
    else:
        print "This is not an odd number."


for nums in odd_numbers:
    max_num = max(odd_numbers)
    print max_num
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top