【问题标题】:How to stop the beginning of my function's loop from repeating如何阻止我的函数循环的开头重复
【发布时间】:2014-11-29 18:47:11
【问题描述】:

我创建了一个代码,用户将在其中输入他们的选择,如果变量“contin”等于“yes”,它将在循环中继续。当用户输入“否”选项或任何其他输入时,它将打印他们的总体答案或错误消息并结束循环。相反,它会重复函数的开头(在这种情况下,它是用户是否想要继续)。我有办法防止这种情况发生吗? 这是代码:

def userinput():

    while True:
        contin = input("Do you wish to continue the game? If so enter 'yes'. If not enter 'no'.")
        if contin == 'yes':
            print(symbol_dictionary["#"]+symbol_dictionary["+"]+symbol_dictionary["/"]+symbol_dictionary["0"]+symbol_dictionary["8"]+symbol_dictionary["4"]+symbol_dictionary["&"]+symbol_dictionary['"']

            guess = input("What symbol do you wish to change? ")

            symbol_dictionary[guess] = input("Input what letter you wish to change the symbol to.(Make sure the letter is in capitals.) ")

            print(symbol_dictionary["#"]+symbol_dictionary["+"]+symbol_dictionary["/"]+symbol_dictionary["0"]+symbol_dictionary["8"]+symbol_dictionary["4"]+symbol_dictionary["&"]+symbol_dictionary['"'])

        elif contin == ('no'):
            print ("These were your overall answers:")
            print(symbol_dictionary["#"]+symbol_dictionary["+"]+symbol_dictionary["/"]+symbol_dictionary["0"]+symbol_dictionary["8"]+symbol_dictionary["4"]+symbol_dictionary["&"]+symbol_dictionary['"'])

            if symbol_dictionary == {"#": "A","+":"C", "/":"Q", "0":"U", "8":"I",
                         "4":"R", "&":"E",'"':'D', "3":"L", "*":"M",
                         "%":"N", "2":"S", ":":"T", "1":"O",",":"J",
                         "$":"K", "!":"H", "7":"Z", "-":"Y", ".":"G",
                         "'":"W",")":"F", "6":"B", "5":"X", "9":"V"}:
                print("Well done! You have completed the game!")

        else:
            print("Please enter a valid input.")

【问题讨论】:

  • 我建议您使用 cmets、pass 或其他方式替换与问题毫无共同之处的代码;这将使您的问题更容易理解。

标签: python function python-3.x python-3.4


【解决方案1】:

你需要做的就是退出函数;在no 分支中添加return

elif contin == ('no'):
    print ("These were your overall answers:")
    print(symbol_dictionary["#"]+symbol_dictionary["+"]+symbol_dictionary["/"]+symbol_dictionary["0"]+symbol_dictionary["8"]+symbol_dictionary["4"]+symbol_dictionary["&"]+symbol_dictionary['"'])

    if symbol_dictionary == {"#": "A","+":"C", "/":"Q", "0":"U", "8":"I",
                 "4":"R", "&":"E",'"':'D', "3":"L", "*":"M",
                 "%":"N", "2":"S", ":":"T", "1":"O",",":"J",
                 "$":"K", "!":"H", "7":"Z", "-":"Y", ".":"G",
                 "'":"W",")":"F", "6":"B", "5":"X", "9":"V"}:
        print("Well done! You have completed the game!")

    # exit the function
    return

【讨论】:

  • 这对我帮助很大。我刚刚开始我的计算机科学 GCSE,我的老师从来没有教过我这个。谢谢:3
【解决方案2】:

只需在 elif 语句的末尾添加一个 break 或 return:

print("Well done! You have completed the game!")
break # or return

这将退出循环。 Break 在这种情况下更有意义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 2018-08-15
    相关资源
    最近更新 更多