1

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 ?

  • Try this - a = input("Enter an int: ") – Tanveer Alam Apr 18 '14 at 13:34
  • 1
    @TanveerAlam NO. Never recomment python2's `input` to a beginner without a very good reason and without explicitly describing all issues like security holes and performance problems this introduces. Especially not when there is a much better alternative available which does not have any of these issues. The correct way here is of course to simply do `int(raw_input("..."))`. – l4mpi Apr 18 '14 at 13:37
  • You should also explain why this is different from using raw_input @TanveerAlam AND put it as an answer though I should imagine this is a duplicate of http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x – Paul Sullivan Apr 18 '14 at 13:37
  • Thanks l4mpi and Paul. – Tanveer Alam Apr 18 '14 at 13:39
  • Thanks! Converting the inputs to int did the job! – user3532810 Apr 18 '14 at 13:46

4 Answers4

3

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)]
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

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
evading
  • 3,032
  • 6
  • 37
  • 57
0

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.
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

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.

Community
  • 1
  • 1
Mp0int
  • 18,172
  • 15
  • 83
  • 114