在python中异常执行异常 [英] unexpected execution of Exceptions in python

查看:218
本文介绍了在python中异常执行异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手蟒蛇,我想出了这个问题。我已经做了一个简单的计算器程序。在add函数中,我使用了try- except。当遇到这个行(如果决定=='否'或决定=='n':),它会显示打印行return(你已经退出)但它也抛出异常。我不明白为什么。

  import sys 

def menu():

打印计算器使用功能
打印选择您的选项:
打印
打印1)加号
打印2)减法
打印3)乘法
打印4)分区
打印5)退出calculator.py
打印
返回输入(选择您的选项:)


def add(a,b):
try:
print a,+,b,=,a + b
打印你要继续吗?
decision = raw_input(yes or no:)
如果决定=='否'或决定=='n':
return(你已退出)
sys.exit(0)
elif决定=='是'或决定=='y':
menu()
untrusted.execute()

除了:
打印错误选择!!!
e = sys.exc_info()[0]
打印错误:%s%e
sys.exit(0)


解决方案

这适用于我....我想, return(你已经退出)是这里的问题,istead的 sys.exit(0) try return 0 我不知道为什么它的一个问题,但它的作品。我尝试了是的,没有条件。

  import sys 



$ d


打印使用功能的计算器
打印选择您的选项:
print
print1)加法
print2)减法
print3)乘法
print4)分区
print5)退出calculator.py
print
返回输入(选择你的选项)


def add(a,b):
try:
print a,+, b,=,a + b
打印你要继续吗?
decision = raw_input(yes or no:)
如果决定=='否'或决定=='n':
returnn
#sys.exit(0)
elif decision =='yes'或决定=='y':
return y
untrusted.execute()

除了:
打印错误选择!!!
e = sys.exc_info()[0]
打印错误:%s%e
sys.exit(0)

while(True) b $ b selected_option = menu()
if(selected_option == 1):
print输入第一个数字:
no1 = raw_input()
print输入第二个数字:
no2 = raw_input()
option = add(no1,no2)
if(option =='y'):
printyes selected
continue
if(option =='n'):
printno selected
sys.exit(0)

输出:
是否要继续:



是或否:re



错误选择!!!



错误:< type'exceptions.NameError'>






是否要继续:



是或否:y



是选择



使用函数的计算器



选择您的选项:



1)加法
2)减法
3)乘法
4)部门
5)退出calculator.py



选择您的选项:






您是否要继续:



是或否:n



没有选择



root @ yogesh-System-model:〜#


I am novice to python and i came up with this problem. i have made a simple program for calculator. In add function, i have used try- except. when this line is encountered (if decide== 'no' or decide== 'n':), it displays the print line "return(" You have exited ")" but it also throws the exception. I cant understand why.

import sys

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):
    try:
        print a, "+", b, "=", a + b
        print " Do you want to continue: "
        decide=raw_input("yes or no: ")
        if decide== 'no' or decide== 'n':
            return(" You have exited ")
            sys.exit(0)
        elif decide=='yes' or decide== 'y':
            menu()
        untrusted.execute()

    except:
        print "wrong choice!!!"
        e = sys.exc_info()[0]
        print "Error: %s" % e
        sys.exit(0) 

解决方案

This works for me ....I suppose return(" You have exited ") is problem here and istead of sys.exit(0) try return 0 I dont know why its a problem but it works. I tried it for both yes and no conditions.

import sys



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):
try:
    print a, "+", b, "=", a + b
    print " Do you want to continue: "
    decide=raw_input("yes or no: ")
    if decide== 'no' or decide== 'n':
        return "n"
        #sys.exit(0)
    elif decide=='yes' or decide== 'y':
        return "y"
    untrusted.execute()

except:
    print "wrong choice!!!"
    e = sys.exc_info()[0]
    print "Error: %s" % e
    sys.exit(0)

while(True):
selected_option=menu()
if(selected_option == 1):
    print "Enter first number:"
    no1 = raw_input()
    print "Enter second number:"
    no2 = raw_input()
    option=add(no1,no2)
    if(option == 'y'):
        print "yes selected"
        continue
    if(option=='n'):
        print "no selected"
        sys.exit(0)

output: Do you want to continue:

yes or no: re

wrong choice!!!

Error: <type 'exceptions.NameError'>


Do you want to continue:

yes or no: y

yes selected

calculator using functions

Choose your option:

1) Addition 2) Subtraction 3) Multiplication 4) Division 5) Quit calculator.py

Choose your option:


Do you want to continue:

yes or no: n

no selected

root@yogesh-System-model:~#

这篇关于在python中异常执行异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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