Python 如何避免在满足条件之前推进循环?
我编写了以下程序:Python 如何避免在满足条件之前推进循环?,python,list,for-loop,Python,List,For Loop,我编写了以下程序: numb= ["First", "Second", "Third"] players=[] n = '' score=[] for i in numb: print("Type the score of the name of the", i, "player:") answer = n = str(input(' ')) players.append(d) for a in players: for b in numb:
numb= ["First", "Second", "Third"]
players=[]
n = ''
score=[]
for i in numb:
print("Type the score of the name of the", i, "player:")
answer = n = str(input(' '))
players.append(d)
for a in players:
for b in numb:
a = int(input("Type the", b, "score of", a))
if a!=0:
score.append(a)
else:
我想避免在numb:中为b推进内部循环,只要a是0。
例如:如果第一个分数为0,程序将再次打印“键入第一个分数…”。我想这就是您想要的:
for a in players:
for b in numb:
done = False
while not done:
a = int(input(f"Type the {b} score of {a}"))
if a :
score.append(a)
done = True
done
开始为False,为True时中断并转到下一个列表元素
离题:我给你的代码添加了一些PEP8,希望你喜欢 在python>=3.8中,您可以使用新添加的:
for a in players:
for b in numb:
while (a := int(input(f"Type the {b} score of {a}")) == 0:
pass
score.append(a)