python中用户输入条件下的Continuos循环? [英] Continuos loop with user input condition in python?

查看:68
本文介绍了python中用户输入条件下的Continuos循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是学习我的第一个python并尝试创建一个具有用户输入条件的连续循环.

I just learn my first python and try to make a continuous loop that has a user input condition.

#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()和另一个if语句之后回想起重复操作

I was thinking if I can recall repeat after I called data_cal() and, or another if statement

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

请多多包涵,我是编程语言的新手,可能不太清楚自己的表达.这个想法是第一次调用data_cal(),然后要求用户输入-(是否要重试?")-如果输入是,则重新调用data_cal(),然后重新询问(您是否想要重试吗?)并重复该循环,如果输入为no,则执行print(" Bye)非常感谢你!

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!

推荐答案

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

使用已编写的代码格式,只需将重复分配行移至data_cal(),然后返回该值即可在while循环中使用该值.

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.

这篇关于python中用户输入条件下的Continuos循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆