Python计算器除以零/负数.诠释崩溃程序 [英] Python Calculator Divide by Zero/Sqrting a Neg. Int. crashing program

查看:84
本文介绍了Python计算器除以零/负数.诠释崩溃程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在为一个项目执行此操作,每当我尝试将其除以零或平方根时,程序就会关闭负数.我试图找到可以插入代码的内容,使其显示一条消息,然后再次提示输入该值,但是我尝试插入的所有内容都会导致该程序在启动时立即关闭.

这里是没有插入任何东西来修复崩溃的计算器.

 导入数学def convertString(str):尝试:returnValue = int(str)除了ValueError:returnValue = float(str)返回returnValuedef加法(a,B):返回convertString(a)+ convertString(B)def减(a,B):返回convertString(a)-convertString(B)def乘法(a,B):返回convertString(a)* convertString(B)def除法(a,B):返回convertString(a)/convertString(B)def sqrt(a):返回math.sqrt(convertString(a))keepProgramRunning = True打印欢迎来到[已删除]的2011年4-H项目!这是一个用Python编码的简单计算器,它是一种高级编程语言.Java,C,C ++和Perl是您可以使用的其他高级编程语言.可能听说过."在keepProgramRunning时:打印 ""打印请选择您想做的事情:"打印 ""打印"1)加法"打印"2)减法"打印"3)乘法"打印"4)司"打印"5)平方根"打印"6)退出程序"选择= raw_input()如果选择=="1":numberA = raw_input(输入您的第一个加数:")numberB = raw_input(输入您的第二个加数:")打印这些数字的总和是:"打印加法(数字A,数字B)elif choice =="2":numberA = raw_input(输入您的第一个词:")numberB = raw_input(输入您的第二个字词:")打印这些数字的区别是:"打印减法(数字A,数字B)elif choice =="3":numberA = raw_input(输入您的第一个因子:")numberB = raw_input(输入您的第二个因子:")打印这些数字的乘积是:"打印乘法(numberA,numberB)elif choice =="4":numberA = raw_input(输入您的股息:")numberB = raw_input(输入您的除数:")打印这些数字的商是:"打印部门(编号A,编号B)elif choice =="5":numberA = raw_input(输入您要查找其平方根的数字:")打印您的结果是:"打印sqrt(numberA)elif choice =="6":打印:再见!感谢您在我的项目以及其他每个人的评审中所花费的时间!祝您有愉快的一天!(。◕‿◕。)"keepProgramRunning = False别的:打印请选择一个有效的选项."打印"\ n" 

我不确定要插入哪些内容以及在哪里解决崩溃问题,但我认为问题出在我的位置.

我一直在尝试插入如下内容:

  ValueError除外:打印您不能被零除.请选择另一个除数."numberB = raw_input(输入您的除数:") 

那行得通吗?我要在哪里插入?如果它不起作用,它将在什么地方去?

我一直试图放在它后面

  numberB = raw_input(输入您的除数:") 

因此该部分将显示为

  elif choice =="4":numberA = raw_input(输入您的股息:")numberB = raw_input(输入您的除数:")除了ValueError:打印您不能被零除.请选择另一个除数."numberB = raw_input(输入您的除数:")打印这些数字的商是:"打印部门(编号A,编号B) 

但是正如我所说,当我尝试打开程序时,它将在打开后立即关闭.另外,我知道,如果他们再次输入0,程序将崩溃.有什么办法可以使其返回到其下一行?

另外,对于关闭程序,在程序显示后立即关闭或同时执行命令时,无法读取应显示的消息.无论哪种方式,都无法读取该消息.有什么方法可以使消息显示在单独的对话框窗口中,从而在关闭窗口时导致程序关闭?还是至少有一种使它延迟关闭的方法?

如果我的术语有误,请纠正我.我对此还是有些陌生.

而且,一如既往,程序的任何部分的(建设性)反馈总是值得赞赏的.

解决方案

问题是您试图在引发异常之前捕获异常.相反,请尝试以下操作:

  elif choice =="4":numberA = raw_input(输入您的股息:")numberB = raw_input(输入您的除数:")而float(numberB)== 0:打印您不能被零除.请选择另一个除数."numberB = raw_input(输入您的除数:")打印这些数字的商是:"打印部门(编号A,编号B) 

Alright, I'm doing this for a project and whenever I attempt to have it divide by zero or square root a negative number that program closes out. I've attempted to find something I can insert into the code to make it display a message and then prompt for the value again, but everything that I've tried inserting causes the program to close out instantly when I start it up.

Here's the calculator without anything inserted to fix the crashes.

import math

def convertString(str):
    try:
        returnValue = int(str)
    except ValueError:
        returnValue = float(str)
    return returnValue

def addition(a, B):
    return convertString(a) + convertString(B)

def subtraction(a, B):
    return convertString(a) - convertString(B)

def multiplication(a, B):
    return convertString(a) * convertString(B)

def division(a, B):
    return convertString(a) / convertString(B)

def sqrt(a):
    return math.sqrt(convertString(a))

keepProgramRunning = True

print "Welcome to [Removed]'s 2011 4-H Project! This is a simple calculator coded in  Python, which is a high-level programming language. Java, C, C++, and Perl are  other high-level programming languages that you may have heard of."

while keepProgramRunning:
    print ""
    print "Please choose what you would like to do:"
    print ""
    print "1) Addition"
    print "2) Subtraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Square Root"
    print "6) Quit Program"

    choice = raw_input()    

    if choice == "1":
        numberA = raw_input("Enter your first addend: ")
        numberB = raw_input("Enter your second addend: ")
        print "The sum of those numbers is:"
        print addition(numberA, numberB)
    elif choice == "2":
        numberA = raw_input("Enter your first term: ")
        numberB = raw_input("Enter your second term: ")
        print "The difference of those numbers is:"
        print subtraction(numberA, numberB)
    elif choice == "3":
        numberA = raw_input("Enter your first factor: ")
        numberB = raw_input("Enter your second factor: ")
        print "The product of those numbers is:"
        print multiplication(numberA, numberB)
    elif choice == "4":
        numberA = raw_input("Enter your dividend: ")
        numberB = raw_input("Enter your divisor: ")
        print "The quotient of those numbers is:"
        print division(numberA, numberB)
    elif choice == "5":
        numberA = raw_input("Enter the number you wish to find the square root of: ")
        print "Your result is:"
        print sqrt(numberA)
    elif choice == "6":
        print "Goodbye! Thank you for your time spent both judging my project and those of everyone else! Have a nice day! (。◕‿◕。)"
        keepProgramRunning = False
    else:
        print "Please choose a valid option."
        print "\n"

I'm not real sure what to insert and where to solve the crashes, but I think the problem lies with my placement.

I've been attempting to insert something like this:

except ValueError:
            print "You cannot divide by zero. Please choose another divisor."
            numberB = raw_input("Enter your divisor: ")

Would that work? Where would I insert it? If it wouldn't work, what would and where would it go?

I've been attempting to put it after

numberB = raw_input("Enter your divisor: ")

So that section would read

elif choice == "4":
    numberA = raw_input("Enter your dividend: ")
    numberB = raw_input("Enter your divisor: ")
        except ValueError:
            print "You cannot divide by zero. Please choose another divisor."
            numberB = raw_input("Enter your divisor: ")
    print "The quotient of those numbers is:"
    print division(numberA, numberB)

But as I said, the program will close as soon as it opens when I do try that. Also, I know that if they inputted 0 again the program would crash. Is there any way to make it return to the line that it's under?

Also, for closing the program, the message it is supposed to display can't be read as the program is closing immediately after it's displayed, or the commands are being executed at the same time. Either way, the message can't be read. Is there any way to make the message appear in a separate dialog window that will cause the program to close when the window is closed? Or at least a way to make it delay before closing?

Please correct me if I got any terminology wrong. I'm still somewhat new to this.

And, as always, (constructive) feedback for any part of the program is always appreciated.

解决方案

The problem is that you are trying to catch an exception before it is thrown. Instead, try this:

elif choice == "4":
    numberA = raw_input("Enter your dividend: ")
    numberB = raw_input("Enter your divisor: ")
    while float(numberB) == 0:
        print "You cannot divide by zero. Please choose another divisor."
        numberB = raw_input("Enter your divisor: ")
    print "The quotient of those numbers is:"
    print division(numberA, numberB)

这篇关于Python计算器除以零/负数.诠释崩溃程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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