473,324 Members | 2,548 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,324 software developers and data experts.

Infinite loops and string comparision in a lists

I am a new programmer, learning python. I have written this code but I need help
1. Make an infinite loop so that the user can enter many numbers into the list.
2. Make a provision for a infinite loop to end, like typing the word "Done".
Here is the code:
Expand|Select|Wrap|Line Numbers
  1. print "Welcome to average script "
  2.  
  3. def main():
  4.     #declaration of an empty list avg
  5.     avg = []
  6.     loop = 1
  7.     while loop ==1:
  8.         try:
  9.  
  10.             avg.append( int(raw_input("Enter the first number you want average: \n")))   
  11.             avg.append( int(raw_input("Enter the second number you want average: \n")))
  12.             avg.append( int(raw_input("Enter the third number you want average: \n")))
  13.  
  14.             print "The list is: ", avg
  15.             ambo = len(avg)
  16.             print "The number of members in the list is: ", ambo
  17.             s = sum(avg)
  18.             print "The sum of the enterd numbers is: ",s
  19.             a = float (s/ambo)
  20.             print "The average of the enterd members of the list are: ", a
  21.             break
  22.         except ValueError:
  23.             print "\n Please Enter a number (Integer)"
  24.  
  25.         try:
  26.             loop = input("Press 1 to try again: ")
  27.             avg = []
  28.         except NameError:
  29.             loop = 1
  30.  
  31.         continue           
  32. main()
Nov 24 '11 #1
2 1976
Glenton
391 Expert 256MB
Hi

Thanks for posting. Here's some methods that might help
Expand|Select|Wrap|Line Numbers
  1. mylist=[]
  2. while 1:
  3.     a=raw_input("enter number: ")
  4.     if a=="done": break
  5.     try:
  6.         mylist.append(float(a))
  7.     except:
  8.         print "invalid input."
  9. print mylist, sum(mylist)/max([len(mylist),1]) 
I haven't tested it but hopefully it makes sense.
Nov 24 '11 #2
Hi Glenton,
Thanks so much, it works perfectly as i expected.
Nov 25 '11 #3

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

Similar topics

1
by: Marcus Liddle | last post by:
<newbie hat> Hi I'm trying to get a really simple python program to run a bash testing script and kill itself if its been running to long (ie infinite loop) create the thread object - test...
6
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some...
7
by: Andrej Hocevar | last post by:
Hello, what is the best way of solving cases where one has to wait (forever, until killed or instructed otherwise) for some event to occur? E.g., wait for data, monitoring a file? In fact, that...
7
by: Rob | last post by:
I'm stuck in a program I'm making. I'm supposed to have a menu that asks the user to choose an option between 1 and 3. If the user chooses a valid option, i.e. types a number from 1 to 3, then...
3
by: priyanka | last post by:
Hi there, I am trying to compare two strings. But I am not able to do so. I use : if(function_name == function_label){ but this does not work. I have written down my code:
10
by: thesayan | last post by:
hello, i am new to visual basic 2005 and i am making a few programs with the do loop function. i am trying to make my prog exit an infinite loop when a key on the keyboard is pressed, but i have...
35
by: user34 | last post by:
(Sorry if this gets posted twice) Hi all. I am trying to learn C.As a simple exercise i tried to write a code which would compare two strings using pointers. However i am not getting the correct...
3
by: cosmos22 | last post by:
The following code below shows an example of a infinite while loop. I was wondering how I would incorperate another infinite loop into this , without ruining the actions inside of it. I would like to...
3
atksamy
by: atksamy | last post by:
Heré is the code I am working on. I am using this to validate the values of tables in a database. i am almost done but i have a infinite loop goin towards the end loop s not ending and the program...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.