def main():
values = []
numbers = get_Values(values)
print("The numbers in the list are:")
print(numbers)
removeList = remove_Value_List(values)
print(removeList)
def get_Values(values): #This asks the user to enter all the values they want to be in the list
again = "y"
while again == "y":
num = float(input("Enter a number:"))
values.append(num)
print("Do you want to add another number?")
again = input("y = yes, anything else = no:")
print()
return values
def remove_Value_List(values): #This asks the user which value they want to remove from the list
print("Here are the numbers in the list:")
print(values)
number_list = input("Which value should I remove?")
try:
values.remove(number_list)
print("Here is the revised list:")
return values
except ValueError:
print("That item is not found in the list.")
number_list = input("which value should I remove?")
main()
How do you get this to remove the value that the user wants to remove from the list and then return the new list values?
num = float(input("Enter a number:"))
values.append(num)
When you add numbers to the list you convert them to floats.
number_list = input("Which value should I remove?")
values.remove(number_list)
When you try to remove them you don't have the float conversion, so it tries to remove the string the user typed. That will never remove anything as the list doesn't contain strings.
If you really want to do the removal in the function then you need to do a loop. otherwise the except would fall through to a return of None. I see that this exits as soon as one value has been removed. I will assum that is the way you want it.
def remove_Value_List(values):
#This asks the user which value they want to remove from the list
print("Here are the numbers in the list:")
print(values)
# Now ask which numbers to remove
while True:
try:
number_list = input("Which value should I remove?")
values.remove(number_list)
print("Here is the revised list:")
return values # You can use break here
except ValueError:
print("That item is not found in the list.")
continue
# If you used break in the while put return values here
# return values if break was used to exit the loop
However, there is another way to do it. You set up two definitions for functions to create the two lists. You should make the function to create the removal list (remove_Value_List()) in the same way that you create the list for the number (get_Values()). You should not check for the members of the removal list to be in the numbers list because these are independent functions and you may add something to the numbers list later. You should write the two functions to run on their own.
After you have completely build the two lists, then go through the remove_List and redo the numbers list. If you want to have this purged list as a third one, rather than changing numbers, first do a copy
newnumbers = numbers[:]
for x in removeList:
if x in newnumbers:
newnumbers.remove(x)
You can of course use the try: except method as well but the above avoids it.
newnumbers = numbers[:]
for x in removeList:
try:
newnumbers.remove(x)
except ValueError:
continue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.