如果用户没有回答“是”或“否”,如何重复我的迭代

我对python相当陌生,正在做一些基本的代码。我需要知道如果答案不是肯定的或否定的,我是否可以重复我的迭代。这是代码(对不起那些认为我做坏习惯的人)。我需要在其他过程中重复迭代。(该函数目前仅输出文本)


    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()


慕妹3242003
浏览 128回答 2
2回答

冉冉说

只需将代码放入循环中: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

森栏

您可以尝试循环,而它不是是或否while remove_char1_attr1 not in ('yes', 'no'):        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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python