如何运行input语句直到在python中得到所需的内容?

2024-03-29 15:53:08 发布

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

我正在运行下面的代码,其中program = MainFormula()定义了程序中使用的函数。我想要实现的是,程序一直持续到输入到问题-“你想改变alpha吗?”?(Yes/No)“不是。我得到了我想要的,但是程序给了我两次”你想改变alpha吗?(是/否)“一旦我以“是”回答问题。有人能帮我避免问“你想改变阿尔法吗?”?(是/否)““是否要更改alpha?”?(是/否)“两次?(只需要一个)

input_value1 = input("Do you want to change alpha? (Yes/No) ").lower() 
while input_value1=="yes":      
    input_value1 = input("Do you want to change alpha? (Yes/No) ").lower()  
    if input_value1 == "yes":          
        program = MainFormula() print(program)   
        else:  
            print("Congratulations! You are done with the task.")

Tags: tono程序alphayouinputprogramchange
2条回答
while True:
    input_value = input("Do you want to change alpha? (Yes/No) ").lower()

    if 'no' in input_value:  # When this condition is met we break from the loop
        break
    elif 'yes' in input_value:
        program = MainFormula()
        print(program)
    else:
        print("Congratulations! You are done with the task.")
        break

我相信这就是你想要达到的目标-如果这不是你想要的,请告诉我,我会相应地调整我的答案。你知道吗

你好像有这个

input_value1 = input("Do you want to change alpha? (Yes/No) ").lower()
while input_value1=="yes":
    input_value1 = input("Do you want to change alpha? (Yes/No) ").lower()
    if input_value1 == "yes":
        program = MainFormula() 
        print(program)
    else:
        print("Congratulations! You are done with the task.")

一个简单的方法是避免问两次问题:

while input("Do you want to change alpha? (Yes/No) ").lower()=="yes":
    program = MainFormula() 
    print(program)

print("Congratulations! You are done with the task.")

相关问题 更多 >