Python 循环和程序重新启动(如果为真) [英] Python looping and program restart if true

查看:21
本文介绍了Python 循环和程序重新启动(如果为真)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙,因为我是一个真正的初学者,很想了解更多.我正在尝试了解有关循环的更多信息,因此请查看此代码.

Please help as I am a true beginner and would love to learn more. I am trying to learn more about looping so take a look at this code please.

# lets the user input first number
num1 = float(raw_input("Enter your first number  ---> "))

# lets the user input second number
num2 = float(raw_input("Enter your second number  ---> "))

#the next four lines sets the basic mathematical equations
addAns = num1+num2
subAns = num1-num2
mulAns = num1*num2
divAns = num1/num2

# ask the user to let the program know what equation to run with an 
option of running them all
operator = raw_input("What operator would you like to use(+,-,*,/ or ALL)")

#these if and else statements are based on the input by the user.         
Depending on the operator chosen it will print that equation.
if operator == "+":
    print "the result of:	 " ,num1, "+" ,num2, "=" ,addAns,
elif operator == "-":
    print "
the result of:	 " ,num1, "-" ,num2, "=" ,subAns,
elif operator == "*":
    print "
the result of:	 " ,num1, "*" ,num2, "=" ,mulAns,
elif operator == "/":
    print "
the result of:	 " ,num1, "/" ,num2, "=" ,divAns,
elif operator == "ALL" or "all" or "All":
    print "the result of:	 " ,num1, "+" ,num2, "=" ,addAns,
    print "
the result of:	 " ,num1, "-" ,num2, "=" ,subAns,
    print "
the result of:	 " ,num1, "*" ,num2, "=" ,mulAns,
    print "
the result of:	 " ,num1, "/" ,num2, "=" ,divAns,

问题是,如果我要让用户告诉我是或否,我该如何让这个程序从头开始:

the question is how do I get this program to start all over if I was to ask the user to tell me yes or no for example:

answer = raw_input("Would you like to try again?(yes or no)")
if answer = "yes"
    then restart?????
else answer = "no"
    print "Thanks for using my calculator!!!"

推荐答案

你可以尝试定义一个函数 calc 或者你想要的任何东西并编写整个该函数中的代码,您可以在需要时直接调用该函数

you can try defining a function calc or what ever u want and write the whole code in that function and u can directly call that function whenever u need

def calc():
 your code

if answer == 'yes' or 'Yes':calc()
elif answer == 'no' or 'No':print "Thanks for using my calculator

这篇关于Python 循环和程序重新启动(如果为真)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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