繁体   English   中英

我如何返回到 Python 脚本中的上一个问题

[英]How do i return to the previous question in Python Script

我是使用 Python 脚本的新手,我一直在使用 Jupyter Notebook,终于意识到它们两者之间的区别。 在这段代码中,我尝试使用 def 创建一个简单的命令,我需要在其中键入数字 1 - 3。

def Twitter():
    while True:
        user_input = input(
            'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')

        if user_input == '1':
            print('You picked Crawling')
            break
        elif user_input == '2':
            print('You picked Verification')
            break
        elif user_input == '3':
            print('Return')
            Opening()
            break
        else:
            print('Type a number 1-3')
            continue

user_input = ''

def Opening():
    while True:
        user_input = input(
            'Pick one: 1) Twitter | 2) Instagram ---->')
    
        if user_input == '1':
            Twitter()
            break
        elif user_input == '2':
            print('Instagram!')
            break
        else:
            print('Type a number 1-2')
            continue

我还很不成熟,当我选择数字 3 时需要帮助,该函数将返回到上一部分而不是根本不输出结果,谢谢!

您可以在函数内部返回函数,而不是使用break语句。

def Twitter():
    while True:
        user_input = input(
            'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')

        if user_input == '1':
            print('You picked Crawling')
            break
        elif user_input == '2':
            print('You picked Verification')
            break
        elif user_input == '3':
            print('Return')
            return Opening()  # switch to Opening()
        else:
            print('Type a number 1-3')
            continue


def Opening():
    while True:
        user_input = input(
            'Pick one: 1) Twitter | 2) Instagram ---->')

        if user_input == '1':
            return Twitter()  # switch to Twitter()
        elif user_input == '2':
            print('Instagram!')
            break
        else:
            print('Type a number 1-2')
            continue


if __name__ == '__main__':
    Opening()

您可以在Twitter()结束时再次调用Opening()函数

def Twitter():
    while True:
        user_input = input(
            'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')

        if user_input == '1':
            print('You picked Crawling')
            break
        elif user_input == '2':
            print('You picked Verification')
            break
        elif user_input == '3':
            print('Return')
            Opening()  # Here
            break
        else:
            print('Type a number 1-3')
            continue

它对我来说很好......也许你可以尝试递归调用函数而不是使用 while true?

def Twitter():
    user_input = input(
        'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')

    if user_input == '1':
        print('You picked Crawling')
        Twitter()
    elif user_input == '2':
        print('You picked Verification')
        Twitter()
    elif user_input == '3':
        print('Return')
        Opening()
    else:
        print('Type a number 1-3')
        Twitter()

user_input = ''

def Opening():
    user_input = input(
        'Pick one: 1) Twitter | 2) Instagram ---->')

    if user_input == '1':
        Twitter()
    elif user_input == '2':
        print('Instagram!')
        Opening()
    else:
        print('Type a number 1-2')
        Opening()

Twitter()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM