问题
I'm learning beginner python and there's one question that I'm stuck on.
The question involves asking user input for any amount of mushrooms that they have picked, entering a weight, and then sorting them according to the user input. For this, a list and a while loop is needed to append the inputs into the list.
Currently I am trying to implement a sentinel value that will stop the while loop after all of the user inputs have been entered, but setting the sentinel as "STOP" conflicts with the int() notification.
if __name__ == "__main__":
STOP = "stop"
mushroom = int(input("Enter a mushroom weight in grams, or STOP to end. "))
total_list = []
while total_list != STOP:
total_list.append(mushroom)
mushroom = int(input("Enter a mushroom weight in grams, or STOP to end. "))
print(total_list)
The program runs well until entering "STOP", where a syntax error appears.
mushroom = int(input("Enter a mushroom weight in grams, or STOP to end. "))
ValueError: invalid literal for int() with base 10: 'STOP'
As you can see, the sentinel value STOP conflicts with my input suggestion, resulting in an error.
As for the second part of the problem, I need to sort the value inputs by weight. If everything has been done correctly, I should have a list with all the values. What kind of code can I use in order to sort the values? I need to sort each integer value based on small (<100), medium (100-1000), and large (>1000), then print the results in a statement. Am a little clueless regarding what I need to do here.
Thank you, just kind of stuck in one place.
回答1:
You can use the error produced by trying to convert a noninteger to an integer to your advantage with try
and except
in Python. Here is the documentation for that.
Rewriting your code, you might try this:
if __name__ == "__main__":
STOP = "STOP"
total_list = []
while True:
try:
user_input = input("Enter a mushroom weight in grams, or STOP to end. ")
total_list.append(int(user_input))
except ValueError:
if user_input.strip().upper()==STOP:
break
else:
print("Incorrect entry! '{}' is not an integer".format(user_input))
Then if you want to sort that list and put into categories, you might consider using a dictionary:
total_list.sort()
di={'Small':[],'Medium':[],'Large':[]}
for e in total_list:
key='Small' if e<100 else 'Medium' if 100<=e<=1000 else 'Large'
di[key].append(e)
print(total_list, sum(total_list),di)
回答2:
The first crash is occurring because you are trying to cast "STOP" as an int. For example, if you open a python interpreter and type int("STOP") you will get the same crash. The interpreter does not know how to convert the string "STOP" to an integer. A better way might be to check if the input string can be converted to an integer with isdigit() or a try/except.
For the filtering, the easiest way would be to use a list comprehension on your lists. Something like this should work:
if __name__ == "__main__":
mushroom = None
total_list = []
while mushroom != "STOP":
mushroom = input("Enter a mushroom weight in grams, or STOP to end. ")
if mushroom.isdigit():
total_list.append(int(mushroom))
sorted_total_list = sorted(total_list)
small_values = [mushroom for mushroom in total_list if mushroom < 100]
medium_values = [mushroom for mushroom in total_list if 100 <= mushroom <= 1000]
large_values = [mushroom for mushroom in total_list if mushroom > 1000]
print(sorted_total_list)
print(small_values)
print(medium_values)
print(large_values)
来源:https://stackoverflow.com/questions/58244212/how-to-use-sentinel-values-different-from-input-sort-list-by-feature