python中带有用户输入条件的Continuos循环?

python中带有用户输入条件的Continuos循环?,python,if-statement,while-loop,user-input,Python,If Statement,While Loop,User Input,我只是学习了我的第一个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

我只是学习了我的第一个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")
我在想,在调用data_cal()and之后,我是否能回忆起repeat,或者另一个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")
请容忍我,我对编程语言非常陌生,可能无法很清楚地表达自己。想法是第一次调用数据_cal(),然后请求用户输入-(“您想再试一次吗?”)-如果输入为是,则调用数据_cal(),然后重新询问(“您想再试一次吗?”)并重复该循环,如果输入为否,则打印(“再见”)
多谢各位

如果用户想再试一次(无论在
data\u cal()
中做了什么),则必须在
循环中询问用户。否则,给出的答案永远不会改变

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")

您可以使用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

根据您编写的代码格式,只需将重复赋值行移动到data_cal(),然后返回值,以便在while循环中使用该值。

您可以在函数中使用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")

您想继续该程序,直到用户输入
'n'
'No'
?我相信您应该能够在函数调用后将
repeat=input()
行复制到循环中,并且不要忘记冒号(:)对于while语句:-)没问题:-)如果您觉得答案有用,请重新投票。如果您在while循环中调用它之前没有定义repeat,这是没有意义的,是吗。。如果我错了,请纠正我。你完全正确。您必须给
repeat
一个合理的默认值,以便进入循环。我会更新答案。
#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")