简体   繁体   English

在 python 中与用户输入条件连续循环?

[英]Continuos loop with user input condition in python?

I just learn my first python and try to make a continuous loop that has a user input condition.我刚刚学习了我的第一个 python 并尝试制作一个具有用户输入条件的连续循环。

#Make the calculating func
def data_cal():
    pennies = int(input("What's your pennies?"))
    dollars = pennies // 100
    cents = pennies % 100
    print("You have $", dollars, "and", cents, "cents")
data_cal()
#User input for answer
repeat = input("Do you want to try again?")
answer = ['yes','YES','Yes','y','Y']
#Loop for answer
while repeat in answer
    data_cal()
else: print("Bye then")

I was thinking if I can recall repeat after I called data_cal() and, or another if statement我在想,在我调用 data_cal() 和,或者另一个 if 语句之后,我是否可以回忆起重复

…..
while repeat in answer
    data_cal()
    if repeat in answer:
      repeat (#this step I tried to recall repeat, is this possible?, any other way to get around this?)
    else: break
print ("Bye then")

Please bear with me, I am very new to programming language and might not express myself very clear.请多多包涵,我对编程语言很陌生,可能表达得不太清楚。 The idea is to call the data_cal() for the first time then, ask for user input -("Do you want to try again?") - if the input is yes then recall data_cal() and then RE ASK ("Do you want to try again?") and repeat the cycle, if the input is no then print("Bye") Thank you very much!我们的想法是第一次调用 data_cal(),然后询问用户输入 -(“你想再试一次吗?”) - 如果输入是,那么调用 data_cal(),然后 RE ASK(“你要再试一次吗?") 并重复循环,如果输入为否,则 print("Bye") 非常感谢!

You have to ask the user inside of the while loop, if he wants to try again (whatever is done in data_cal() ).您必须在while循环内询问用户,如果他想再试一次(无论在data_cal()中做了什么)。 Otherwise the given answer can never change.否则给定的答案永远不会改变。

answer = ['yes','YES','Yes','y','Y']
repeat = 'yes'

#Loop for answer
while repeat in answer
    data_cal()
    repeat = input("Do you want to try again?")
else: print("Bye then")

You can use continue and breake to control your loop,then you can write like this你可以使用 continue 和 breake 来控制你的循环,然后你可以这样写

#Make the calculating func
def data_cal():
    pennies = int(input("What's your pennies?"))
    dollars = pennies // 100
    cents = pennies % 100
    print("You have $", dollars, "and", cents, "cents")

answer = ['yes','YES','Yes','y','Y']
#Loop for answer
while True:
    data_cal()
    repeat = input("Do you want to try again?")
    if repeat in answer:
        data_cal()
        continue
    else:
        print("Bye then")
        break
#Make the calculating func
repeat = ""
def data_cal():
    pennies = int(input("What's your pennies?"))
    dollars = pennies // 100
    cents = pennies % 100
    print("You have $", dollars, "and", cents, "cents")
    repeat = input("Do you want to try again?")
    return repeat

repeat = data_cal()
#User input for answer
answer = ['yes','YES','Yes','y','Y']
#Loop for answer

while repeat in answer:
    repeat = data_cal()
else:
    print("Bye then")

From the code format you have written, just move the repeat assignment line to data_cal(), and return the value so that you can use that in the while loop.从您编写的代码格式中,只需将重复赋值行移至 data_cal(),并返回该值,以便您可以在 while 循环中使用它。

You can use the while loop inside the function.您可以在 function 中使用 while 循环。

#Make the calculating func
    repeat = "yes"
    answer = ['yes','YES','Yes','y','Y']

    def data_cal():
        global repeat
        while repeat in answer:
            pennies = int(input("What's your pennies?"))
            dollars = pennies // 100
            cents = pennies % 100
            print("You have $", dollars, "and", cents, "cents")
            repeat = input("Do you want to try again?")

    data_cal()
    print("Bye then")

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

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