我正在尝试创建一个程序,其中用户将3个水果和3个非水果输入到两个不同的列表中。
用户首先通过输入“水果”或“非水果”来选择第一个列表。 然后,用户输入每个合格项目,直到第一个列表填满。
我的问题是,一旦第一个选定的列表已满,程序就会结束。 我希望系统提示用户将数据输入另一个列表,直到它也已满为止。
我认为添加“ while len(fruits)<3和len(notfruits)<3”可以有效,但似乎没有什么不同。
我该怎么做?
fruits = []
notfruits = []
print(fruits)
print(notfruits)
print("Please enter fruits or notfruits:")
y = str(input(": "))
while len(fruits) < 3 and len(notfruits) < 3:
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
答案 0 :(得分:1)
or
代替and
y
将永远不会改变这是我的意思:
fruits = []
notfruits = []
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")