473,597 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

6
1523
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 subclassing a built in type: --- Here's an example of a simple dict subclass, which provides a "default value" that is returned when a missing key is requested: class defaultdict(dict):
15
6569
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
2136
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 table header (to select all the checkboxes in table), the script demands at least 3 minutes to execute. I tested the same script in Mozilla and the response time is much more faster. Does anyone has an idea? <script> function...
4
2127
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) { /* main bulk of code goes here */ } }
19
1094
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
1455
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 much memory as I can.
10
3962
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 than 'while'? C: for(i=0; i<length; i++) python: while i < length:
2
2383
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 progress. So I use a shared variable in calculation thread to allow main thread read it periodically in order to show the progress. Quesion comes out. If I just wait and don't make any operation during the whole calculation, the main form is OK,...
10
1338
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 <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523048>. Thanks, Dick Moores =====================================================
8
7247
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 <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
7962
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7884
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8267
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8380
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6681
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5844
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.