如何使用不同于输入& amp;的哨兵值按功能排序列表? [英] How to use sentinel values different from input & sort list by feature?
问题描述
我正在学习初学者python,我遇到了一个问题.
I'm learning beginner python and there's one question that I'm stuck on.
问题涉及询问用户输入的蘑菇量,输入重量,然后根据用户输入对蘑菇进行分类.为此,需要一个列表和while循环,以将输入追加到列表中.
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.
当前,我正在尝试实现一个哨兵值,该值将在输入所有用户输入后停止while循环,但是将哨兵设置为"STOP"会与int()通知冲突.
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)
程序运行良好,直到输入"STOP"(出现语法错误)为止.
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'
如您所见,前哨值STOP与我的输入建议冲突,从而导致错误.
As you can see, the sentinel value STOP conflicts with my input suggestion, resulting in an error.
对于问题的第二部分,我需要按权重对输入的值进行排序.如果一切都正确完成,我应该列出所有值. 我可以使用哪种代码对值进行排序? 我需要根据小(<100),中(100-1000)和大(> 1000)对每个整数值进行排序,然后将结果打印在语句中. 我在这里需要做什么有点不知所措.
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.
推荐答案
您可以使用通过尝试使用Python中的try
和except
将非整数转换为整数而产生的错误.这是文档.
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)
这篇关于如何使用不同于输入& amp;的哨兵值按功能排序列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!