python - 如果用户没有回答是或否,我如何重复我的迭代
问题描述
我对python相当陌生,正在做一些基本的代码。如果答案不是肯定的,我需要知道是否可以重复我的迭代。这是代码(对那些认为我有坏习惯的人感到抱歉)。我需要在 else 期间重复迭代。(该函数目前只输出文本)
if remove_char1_attr1 = 'yes':
char1_attr1.remove(min(char1_attr1))
char1_attr1_5 = random.randint(1,6)
char1_attr1.append(char1_attr1_5)
print("The numbers are now as follows: " +char1_attr1 )
elif remove_char1_attr1 = 'no'
break
else:
incorrect_response()
解决方案
只需将代码放入循环中:
while True:
if remove_char1_attr1 = 'yes':
char1_attr1.remove(min(char1_attr1))
char1_attr1_5 = random.randint(1,6)
char1_attr1.append(char1_attr1_5)
print("The numbers are now as follows: " +char1_attr1 )
elif remove_char1_attr1 = 'no'
break
else:
#incorrect_response()
print("Incorrect")
然后它会一直运行到remove_char1_attr1
“否”