473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I have a question about While loops

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 2400
bvdet
2,851 Expert Mod 2GB
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
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 Expert 512MB
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 Expert Mod 2GB
As I said:
raw_input() returns a string. You must convert the string to an integer.
Oct 24 '08 #5
boxfish
469 Expert 256MB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Steve M | last post by:
1. Near the beginning of the document "Unifying types and classes in Python 2.2" by GvR, which can be found at http://www.python.org/2.2.2/descrintro.html, the author writes the following about...
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
7
by: Gui Lloyd | last post by:
I have a problem with performance in IE. The script above works quite fine when the table has a small number of elements, but, when the table has 2500 elements, when I click in the checkbox of the...
4
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) {...
19
by: C# Learner | last post by:
I've been told on here that the following is equivalent: <snippet 1> int a; for (int i = 0; i < 10; ++i) { a = GetValueFromSomewhere(); } </snippet 1>
1
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
2
by: campos | last post by:
Hi all, I ran into a headache problem. I have a windows form with a progress bar on it. Then I new a thread to do calculation for a long time. I want the progress bar to show the calculation...
10
by: Dick Moores | last post by:
I'm still trying to understand classes. I've made some progress, I think, but I don't understand how to use this one. How do I call it, or any of its functions? It's from the Cookbook, at...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.