【问题标题】:Return input value inside a function [duplicate]返回函数内的输入值[重复]
【发布时间】:2019-01-26 10:23:36
【问题描述】:

如果有人输入错误,我需要帮助,它应该在 python3 中再次返回问题。

我试图返回变量。

def main():
    this = input ('Is this your ..? (Yes/No)')
    if this != 'Yes' and this != 'No':
        print ('please provide a valid answer')

我想一次又一次地问这个问题,直到答案是“是”或“否”。

【问题讨论】:

    标签: python function variables input


    【解决方案1】:

    加入一些while循环:

    我还要记住,在这种情况下,它是区分大小写的。因此,如果有人回答“是”或“是”而不是“是”,它会不断询问

    def main():
        this = input ('Is this your ..? (Yes/No)')
        while this != 'Yes' and this != 'No':
            print ('please provide a valid answer')
            this = input ('Is this your ..? (Yes/No)')
    

    【讨论】:

    • 抱歉,这不是我发布的整个函数,如果我循环该函数,会出现其他输入。
    • 这是您发布的整个功能,它回答了您“一次又一次地提出问题,直到答案为是或否”的直接问题。如果还有更多内容,那么您需要将其包含在您的原始问题中。
    • @chitown88,做while在函数内部循环,所以OP可以在循环之前或之后有其他输入仍然在函数内部。
    • @Austin,好电话
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 2018-07-09
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多