Program freezes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FlamingoRider
    New Member
    • Feb 2009
    • 11

    #1

    Program freezes

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

    Code:
    myFears=[]
    def firstFear():
        if len(myFears) < 1:
            print "You have not entered any fears yet. Please enter one now."
            fear=raw_input("What is a fear of yours?")
            myFears.append(fear)
            return
    
    def fearList():
        Running = True
        print "Your current fears are", myFears
        toDo=raw_input("Do you want to delete a fear, add one, or leave? (del, add, lev)")
        while Running:
            if toDo =="add" "ADD" "Add":
                addFear=raw_input("What is your fear?")
                myFears.append(addFear)
            elif toDo == "del" "DEL" "Del":
                delFear=raw_input("What fear have you overcome?")
                myFears.remove(delFear)
                print "Fear has been deleted"
            elif toDo == "lev" "LEV" "Lev":
                print "Goodbye"
                Running = False
                break
            else:
                 global toDo
    
    firstFear()
    fearList()
    Thanks for the help.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Your code makes comparisons that will never return True, so it enters an infinite loop.
    Code:
    >>> todo = "add" "ADD" "Add"
    >>> todo
    'addADDAdd'
    >>> if todo == "add" "ADD" "Add":
    ... 	print 1
    ... 	
    1
    >>>
    Try this:
    Code:
    if toDo in ["add", "ADD", "Add"]
    or try Boxfish's suggestion to use string method lower() in your other thread.

    Comment

    • FlamingoRider
      New Member
      • Feb 2009
      • 11

      #3
      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.

      Code:
      myFears = []
      def firstFear():
          print "You have not entered any fears yet. Please enter one now."
          print
          fear=str.upper(raw_input("What is a fear of yours?"))
          myFears.append(fear)
          return
      
      def fearList():
          print
          toDo=str.upper(raw_input("Do you want to delete a fear, add one, or leave? (del, add, lev)"))
          print
          if toDo == "ADD":
              addFear=str.upper(raw_input("What is your fear?"))
              myFears.append(str.upper(addFear))
              print str.upper(addFear), "has been added to the list." 
              print myFears
              print
              return
          elif toDo == "DEL":
              delFear=str.upper(raw_input("What fear have you overcome?"))
              print str.upper(delFear), "has been deleted"		
              for i in myFear:
                  if i == delFear:
                      del myFear
                  print myFears
                  print
              else:
                  print "That is not on the list"     
                  print
          elif toDo == "LEV":
              print "Goodbye"
          else:
              pass
      
      firstFear()
      fearList()
      Trying to delete gives an UnboundLocalErr or: local variable myFear referenced before assignment

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        This line should give you the UnboundLocalErr or:
        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
        Code:
        for myFear in myFears:
            if myFear == delFear:
                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:
        Code:
        for i in xrange(len(myFears)):
            if myFears[i] == delFear:
                del myFears[i]
                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:
        Code:
        elif toDo == "LEV":
            print "Goodbye"
            break
        I hope this helps. I know how frustrating it can be to learn a programming language.
        Last edited by boxfish; Feb 22 '09, 04:48 PM. Reason: Wrote def instead of del.

        Comment

        • FlamingoRider
          New Member
          • Feb 2009
          • 11

          #5
          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 :(

          Comment

          Working...