如何编写循环而不被冗余?

2024-07-08 08:37:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使import成为可选的模块,但我无法以更简单的方式重写代码(其工作原理如下):

import sys
sys.path.append('\python_work\chemistry\import')

prompt = "Would you like to check if calculus worked as it should? (yes/no) "
if prompt == 'yes':
    import normal_termination
elif prompt == 'no':
    # pale_green + end are from a colouring module
    print(pale_green + "Skipping to the next step" + end)
else:
    active = True
    while active:
        message = input(prompt)

        if message == 'yes':
            active = False
            import normal_termination
        elif message == 'no':
            active == False
            print(pale_green + "Skipping to the next step" + end)
            break

我尝试了很多不同的方法,但都不管用。 我想让它做的是:问一个简单的问题。如果回答=是->import;如果答案=否->print(message);如果回答!=是的,回答!=否,重复用户输入


Tags: tonoimportmessageifsysgreenprompt
1条回答
网友
1楼 · 发布于 2024-07-08 08:37:03

也许是这样的:

import sys
sys.path.append('\python_work\chemistry\import')

prompt = ''
while prompt != 'yes' and prompt != 'no':
    prompt = input("Would you like to check if calculus worked as it should? (yes/no) ")
    if prompt == 'yes':
        import normal_termination
    elif prompt == 'no':
        print("Skipping to the next step")

相关问题 更多 >

    热门问题