我希望用户在集合 [1, 100] 中输入 10 个唯一整数。到目前为止,这是我的代码:
user_list = []
print "\nChoose any 10 discrete integers in the set [1, 100]. Do not choose duplicates."
i = 1
while i < 11:
try:
number_choice = int(raw_input("\nNumber %d?\n> " % i))
if (0 <= number_choice <= 100) and isinstance(number_choice, int):
i += 1
user_list.append(number_choice)
print "Your list so far: %r" % user_list
elif (number_choice < 0) or (number_choice > 100):
print "'I said to keep it in the set [1, 100].'"
pass
else:
pass
except ValueError:
print "'That isn't a discrete integer, is it?'"
print sorted(user_list)
为了防止重复,我想将第一个更改为if
:
if (0 <= number_choice <= 100) and (isinstance(number_choice, int)) and (number_choice not in user_list):
"Number %d?" % i
这将起作用,除非如果用户输入重复项,它只会再次重复提示。如何修改此代码,使其首先显示提示'I said no duplicates.'
,然后恢复循环?