remove element from list - problem

This is the place for queries that don't fit in any of the other categories.

remove element from list - problem

Postby bluepanido » Fri Jul 19, 2013 1:01 pm

Hi all,
I'm trying to remove an element from a list, based on user input. However, it just removes the element from the last position in the list, whether the user enters a value which is in the list or not. The offending piece of code is:

Code: Select all
        weigth = float(input("\nWhich weight would you like to delete? "))
        if weight in weights:
            weights.remove(weight)
        else:
            print("\nthat weight is not present")


Can anyone help? thanks. Here is the full program if it helps:

Code: Select all
weights = []

menu = None

while menu != "0":
      print("""
      Welcome to the fish weight recorder
      What would you like to do?

      0 - exit
      1 - add a weight
      2 - delete a weight
      3 - show weights

      """)
     
      menu = input("Choice: ")

      if menu == "1":
        weight = float(input("\nPlease enter the weight: "))
        weights.append(weight)

      elif menu == "2":
        weigth = float(input("\nWhich weight would you like to delete? "))
        if weight in weights:
            weights.remove(weight)
        else:
            print("\nthat weight is not present")

      elif menu == "3":
        print("\nYour weights: ")
        for weight in weights:
            print(weight)

      else:
        print("\nThat is not a valid menu choice\n")

print("Press enter to exit\n")

input()
bluepanido
 
Posts: 7
Joined: Thu Jul 11, 2013 12:18 pm
Location: UK

Re: remove element from list - problem

Postby Yoriz » Fri Jul 19, 2013 1:17 pm

You have a typo.
Code: Select all
weigth = float(input("\nWhich weight would you like to delete? "))

weigth is weight everywhere else.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: remove element from list - problem

Postby bluepanido » Fri Jul 19, 2013 1:39 pm

I checked for typos several times. How did I miss that? :oops:

Thank you
bluepanido
 
Posts: 7
Joined: Thu Jul 11, 2013 12:18 pm
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests