By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Program freezes

FlamingoRider
P: 11
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
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

FlamingoRider
P: 11
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
Expert 100+
P: 469
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

FlamingoRider
P: 11
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

Post your reply

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