473,597 Members | 2,275 Online

# I have a question about While loops

2 New Member
This is the question:

"The user must enter a non-negative number to add to the lists or a negative number to stop the program. For every non-negative number entered, place each even and odd number in its respective list. Print the lists of even and odd numbers at the end of the program. You must use a while loop."

This is my code and algorythm:
Expand|Select|Wrap|Line Numbers
1. #create empty lists to hold even and odd numbers
2. even = ""
3. odd = ""
4. #create a message giving the directions
5. mess = "Enter a non-negative number to add to the lists or a negative number to stop"
6. #get input from the user
7. val = raw_input(mess)
8. #while input does not equal sentinal
9. while (val > 0):
10.     #process the data using if statements
11.     if ((val%2) == 0):
12.         even = even + val
13.     else:
14.         odd = odd + val
15.     #get next value
16.     val = raw_input(mess)
17. #print lists
18. print even
19. print odd
20.
I keep getting an error message when I run the program. Does anybody know what is wrong?
Oct 23 '08 #1
5 2408
bvdet
2,851 Recognized Expert Moderator Specialist
This is the question:

"The user must enter a non-negative number to add to the lists or a negative number to stop the program. For every non-negative number entered, place each even and odd number in its respective list. Print the lists of even and odd numbers at the end of the program. You must use a while loop."

This is my code and algorythm:
Expand|Select|Wrap|Line Numbers
1. #create empty lists to hold even and odd numbers
2. even = ""
3. odd = ""
4. #create a message giving the directions
5. mess = "Enter a non-negative number to add to the lists or a negative number to stop"
6. #get input from the user
7. val = raw_input(mess)
8. #while input does not equal sentinal
9. while (val > 0):
10.     #process the data using if statements
11.     if ((val%2) == 0):
12.         even = even + val
13.     else:
14.         odd = odd + val
15.     #get next value
16.     val = raw_input(mess)
17. #print lists
18. print even
19. print odd
20.
I keep getting an error message when I run the program. Does anybody know what is wrong?
raw_input() returns a string. You must convert the string to an integer. You mentioned lists. You are initializing two strings, but you should initialize two lists.

Expand|Select|Wrap|Line Numbers
1. aList = []
To add list elements, use the list method append().
Oct 23 '08 #2
imish06
2 New Member
raw_input() returns a string. You must convert the string to an integer. You mentioned lists. You are initializing two strings, but you should initialize two lists.

Expand|Select|Wrap|Line Numbers
1. aList = []
To add list elements, use the list method append().

I tried that and it didn't work. I keep getting the same message: "not all arguments converted during string formatting."

Expand|Select|Wrap|Line Numbers
1. #creat empty lists to hold even and odd numbers
2. even = []
3. odd = []
4. #create a message giving the directions
5. mess = "Enter a non-negative number to add to the lists or a negative number to stop"
6. #get input from the user
7. val = raw_input(mess)
8. #while input does not equal sentinal
9. while (x > 0):
10.     #process the data using if statements
11.     if(x%2 ==0):
12.         even.append(val)
13.     else:
14.         odd.append(val)
15.     #get next value
16.     val = raw_input(mess)
17. #print lists
18. print even
19. print odd
Oct 23 '08 #3
Laharl
849 Recognized Expert Contributor
You need to use input() (or int(raw_input() ) ), since raw_input gives you a string. The % operator, when applied to strings, is the string formatting operator, used like this:

Expand|Select|Wrap|Line Numbers
1. str = '%d+%d=%d' % (1, 1, 2)
2. print str #Prints 1+1=2
3.
Thus, Python thinks you're trying to format a string when you're really trying to use modulus on an integer.
Oct 23 '08 #4
bvdet
2,851 Recognized Expert Moderator Specialist
As I said:
raw_input() returns a string. You must convert the string to an integer.
Oct 24 '08 #5
boxfish
469 Recognized Expert Contributor
Are you familliar with different variable types in Python? raw_input returns a string. For example, let's say you enter 3. Then without converting it to an int, val will hold the string "3". Since it is a string, you will get very strange results if you treat it like a number. For example, val * 5 is "33333". Five threes. So you need to convert it to an integer with the int function:
Expand|Select|Wrap|Line Numbers
1. val = int(raw_input(mess))
2.
Because "3" is not the same as 3.
Hope this makes a whole lot of sense. :-) Good luck.
Oct 24 '08 #6