我是python的新手,虽然它是一个简单的程序,但我仍然坚持一点 我使用'Return False'移出函数但我想完全退出我的函数。如何做到这一点。 另外,如果我想从python shell运行这个脚本,它是如何完成的。
def menu():
print "calculator using functions"
print "Choose your option:"
print " "
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Quit calculator.py"
print " "
return input ("Choose your option: ")
def add(a,b):
print a, "+", b, "=", a + b
print " Do you want to continue: "
decide=raw_input("yes or no: ")
if decide== "no" or decide== 'n':
print(" You have exited ")
return False
elif decide=='yes' or decide== 'y':
menu()
else:
print "wrong choice!!!"
return False
# this subtracts two numbers given
def sub(a,b):
print b, "-", a, "=", b - a
# this multiplies two numbers given
def mul(a,b):
print a, "*", b, "=", a * b
# this divides two numbers given
def div(a,b):
print a, "/", b, "=", a / b
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add first No: "),input("Add second No: "))
elif choice == 2:
sub(input("Add first No: "),input("Add second No: "))
elif choice == 3:
mul(input("Add first No: "),input("Add second No: "))
elif choice == 4:
div(input("Add first No: "),input("Add second No: "))
elif choice == 5:
loop = 0
print "End of program!"
答案 0 :(得分:0)
您想使用exit
。它退出了该计划。
import sys
def spam():
.
.
.
if some_condition:
sys.exit(0) # exits from the program
.
.
.
答案 1 :(得分:0)
您不必明确return
任何东西退出函数。当解释器到达功能块的末尾时,该功能退出。
答案 2 :(得分:0)
在命令行上输入:
python myprogram.py
从提示符运行程序,如果你想使用特定的python shell(bash或cmd除外),那么你需要查看该特定shell的文档(例如http://www.dreampie.org/)。
退出函数使用:
return
要退出程序,请使用:
import sys
sys.exit(0)