How to make it so that when dividing by 0 issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lisv
    New Member
    • Jun 2020
    • 1

    How to make it so that when dividing by 0 issue

    Code:
    while True:
        print("————————————————————————————————————————")
        print("вміння:")
        print("напеши '+'  щоб додати")
        print("напеши '-'  щоб відняти")
        print("напиши '*' щоб умножиити")
        print("напиши '/' щоб поділити")
        print("напиши 'стоп' щоб все зупинити")                                   
        print("___________________________________")
        user_input = input("")
    
        if user_input == 'стоп':
            break
        elif user_input == '+':
            ...
        elif user_input == '-':
            ...
        elif user_input == '*':
            ...
        elif user_input == '/':
            ...
        else:
            print("user input error")
            #...........
        if user_input == '+':
            num1 = float(input("відіть цифру: "))
            #веденя 1 цифри  
            num2 = float(input("відіть цифру: "))
            #веденя 2 цифри 
            rvn = (num1 + num2)
            #операция додаваня чифри 1 и уифри 2
            print(num1)
            print("+")
            print(num2)
            print("=")
            print(rvn)
            #для оринтаци користвача 
          #............. 
        elif user_input == '-':
             num1 = float(input('видіть цифру:'))
             num2 = float(input("видіть цифру:")) 
             rvn = (num1 - num2)
             print(num1)
             print("-")
             print(num2)
             print("=")
             print(rvn)
    #в цому болоку все таке саме як і в првому тільки знак - зивнив знак +
     #...........
        elif user_input == '*':
             num1 = float(input('видіть цифру:'))
             num2 = float(input("видіть цифру:")) 
             rvn = (num1 * num2)
             print(num1)
             print("*")
             print(num2)
             print("=")
             print(rvn) 
             #.........
        elif user_input == '/':
             num1 = float(input('видіть цифру:'))
             num2 = float(input("видіть цифру:")) 
             rvn = (num1 / num2)
             print(num1)
             print("/")
             print(num2)
             print("=")
             print(rvn)
    Last edited by Banfa; Jun 20 '20, 10:53 PM. Reason: Added code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I assume you mean how to prevent a divide by zero error. Just put a test in the division branch and test the divisor for 0 before doing the calculation.

    Also this code has a horrible amount of repeated code, you could easily cut out 20+ lines by doing the common things ( getting user input, giving output to the user) in a common manner.

    Comment

    • Ishan Shah
      New Member
      • Jan 2020
      • 47

      #3
      You should handle the exceptions using the following code to prevent your program from divide by zero error :

      Code:
      try:
          rvn = (float(num1) / float(num2))
          print(num1)
          print("/")
          print(num2)
          print("=")
          print(rvn)
      except ZeroDivisionError:
          print('0')

      Comment

      • hussainmujtaba
        New Member
        • Apr 2020
        • 13

        #4
        You can use Exception handling (using try and except) to handle a divide by zero error. The concept of exception handling is similar in all languages.You can go through the java exception handling and python exception handling articles to check for yourself.
        Last edited by Rabbit; Sep 1 '20, 03:43 PM.

        Comment

        Working...