473,385 Members | 1,834 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,385 software developers and data experts.

Program freezes

FlamingoRider
When my program enters the fearList() function it will sit at a blank line and I'm unable to enter anything.

Expand|Select|Wrap|Line Numbers
  1.  
  2. myFears=[]
  3. def firstFear():
  4.     if len(myFears) < 1:
  5.         print "You have not entered any fears yet. Please enter one now."
  6.         fear=raw_input("What is a fear of yours?")
  7.         myFears.append(fear)
  8.         return
  9.  
  10. def fearList():
  11.     Running = True
  12.     print "Your current fears are", myFears
  13.     toDo=raw_input("Do you want to delete a fear, add one, or leave? (del, add, lev)")
  14.     while Running:
  15.         if toDo =="add" "ADD" "Add":
  16.             addFear=raw_input("What is your fear?")
  17.             myFears.append(addFear)
  18.         elif toDo == "del" "DEL" "Del":
  19.             delFear=raw_input("What fear have you overcome?")
  20.             myFears.remove(delFear)
  21.             print "Fear has been deleted"
  22.         elif toDo == "lev" "LEV" "Lev":
  23.             print "Goodbye"
  24.             Running = False
  25.             break
  26.         else:
  27.              global toDo
  28.  
  29. firstFear()
  30. fearList()
  31.  
  32.  
Thanks for the help.
Feb 22 '09 #1
4 1979
bvdet
2,851 Expert Mod 2GB
Your code makes comparisons that will never return True, so it enters an infinite loop.
Expand|Select|Wrap|Line Numbers
  1. >>> todo = "add" "ADD" "Add"
  2. >>> todo
  3. 'addADDAdd'
  4. >>> if todo == "add" "ADD" "Add":
  5. ...     print 1
  6. ...     
  7. 1
  8. >>> 
Try this:
Expand|Select|Wrap|Line Numbers
  1. if toDo in ["add", "ADD", "Add"]
or try Boxfish's suggestion to use string method lower() in your other thread.
Feb 22 '09 #2
O.K. I've been trying at this for awihle now and I think I'm about to lose it lol.

I can't figure a way to properly loop fearList() so the LEV sequence will work. I also can't get the DEL sequence to work. The only thing that looks like it works correctly is adding new fears.

Any insight will be of great relief.

Expand|Select|Wrap|Line Numbers
  1. myFears = []
  2. def firstFear():
  3.     print "You have not entered any fears yet. Please enter one now."
  4.     print
  5.     fear=str.upper(raw_input("What is a fear of yours?"))
  6.     myFears.append(fear)
  7.     return
  8.  
  9. def fearList():
  10.     print
  11.     toDo=str.upper(raw_input("Do you want to delete a fear, add one, or leave? (del, add, lev)"))
  12.     print
  13.     if toDo == "ADD":
  14.         addFear=str.upper(raw_input("What is your fear?"))
  15.         myFears.append(str.upper(addFear))
  16.         print str.upper(addFear), "has been added to the list." 
  17.         print myFears
  18.         print
  19.         return
  20.     elif toDo == "DEL":
  21.         delFear=str.upper(raw_input("What fear have you overcome?"))
  22.         print str.upper(delFear), "has been deleted"        
  23.         for i in myFear:
  24.             if i == delFear:
  25.                 del myFear
  26.             print myFears
  27.             print
  28.         else:
  29.             print "That is not on the list"     
  30.             print
  31.     elif toDo == "LEV":
  32.         print "Goodbye"
  33.     else:
  34.         pass
  35.  
  36. firstFear()
  37. fearList()
  38.  
Trying to delete gives an UnboundLocalError: local variable myFear referenced before assignment
Feb 22 '09 #3
boxfish
469 Expert 256MB
This line should give you the UnboundLocalError:
for i in myFear:
There is no list called myFear to loop through. Either use
for i in myFears:
or
for myFear in myFears:

But the following code won't delete anything from myFears
Expand|Select|Wrap|Line Numbers
  1. for myFear in myFears:
  2.     if myFear == delFear:
  3.         del myFear
because myFear is just a copy of one of the elements of myFears.

What you have to do is really ugly. You need to loop through all the list indices, not the list items. So:
Expand|Select|Wrap|Line Numbers
  1. for i in xrange(len(myFears)):
  2.     if myFears[i] == delFear:
  3.         del myFears[i]
  4.         break
The break statement exits the for loop as soon as the fear is found, otherwise, with one of the items missing, the indices will go out of the bounds of the array.

As for leaving, you can use a break statement here too, to exit the while loop:
Expand|Select|Wrap|Line Numbers
  1. elif toDo == "LEV":
  2.     print "Goodbye"
  3.     break
I hope this helps. I know how frustrating it can be to learn a programming language.
Feb 22 '09 #4
Well that did it! It works perfectly now lol. Thanks so much for all the help. I didn't know about the xrange function, I'll have to look that up. And putting [i] after myFears, I wouldn't have thought about it. I have a lot more reading to do I guess :(
Feb 22 '09 #5

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

Similar topics

0
by: Enterprise | last post by:
Hi, I use Access 2000. FEBE design. Tables on server, and everything else on local machine. I have 1 table with a primary key ID field and a text field with names in it. This table is like a...
9
by: kwindham | last post by:
This program doesn't seem like it should be too hard, but I cannot figure it out. Here is the assignment: Password Verifier - Write a program to verify passwords, satisfying the following...
0
by: Daniel | last post by:
C# windows service freezes on System.Diagnostics.Process.Start(info) When I launch PSCP from a C# windows service and launch pscp 0.53 there are no issues. but when I use C# windows service to...
0
by: genojoe | last post by:
I am running an application that, when not used, just sits there firing a BackgroundWorker every 20 seconds. Every now and then, the BackgroundWorker freezes between the DoWork and...
4
by: genojoe | last post by:
My development computer (1 gig, XP Home Edition SP2) freezes when I am editing a very large VB.NET project. It can take minutes to move between a code pane and the Find and Replace pane. On...
0
by: Marcin Szarek | last post by:
Hi! For a few months we suffer mysterious problem with Oracle 10g RAC (more details on server configuration at the bottom). At regular basis (every 5 minutes) nodes of our cluster "freeze" -...
8
by: Marcus | last post by:
I have this application I have made that I launch when the user logs into Windows XP. I would like to delay the launch of the application so that it starts 1 minute after the user has logged in. ...
2
by: =?Utf-8?B?dG9ueSBsb2Nr?= | last post by:
Has anybody else noticed that this webpage freezes when viewed with IE7 on a wide screen (1920*1200) when IE7 is maximised. Or is it just me, using Vista Ultimate by the way.
1
by: xfroggy | last post by:
Alright guys, If someone could give me some ideas or examples if possible since I'm really stuck on this one without a clue how to proceed. I'm trying to write a little logging program for my...
2
by: jaddle | last post by:
I program I'm writing crashes when it exits with a segfault. I'm runnning under linux. Using gdb, i see that the crash happens after the last line of main(), which is just a return(EXIT_SUCCESS); If...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.