By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,067 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,067 IT Pros & Developers. It's quick & easy.

How to make it so that when dividing by 0 issue

P: 1
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     print("覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧")
  3.     print("вміння:")
  4.     print("напеши '+'  щоб додати")
  5.     print("напеши '-'  щоб відняти")
  6.     print("напиши '*' щоб умножиити")
  7.     print("напиши '/' щоб поділити")
  8.     print("напиши 'стоп' щоб все зупинити")                                   
  9.     print("___________________________________")
  10.     user_input = input("")
  11.  
  12.     if user_input == 'стоп':
  13.         break
  14.     elif user_input == '+':
  15.         ...
  16.     elif user_input == '-':
  17.         ...
  18.     elif user_input == '*':
  19.         ...
  20.     elif user_input == '/':
  21.         ...
  22.     else:
  23.         print("user input error")
  24.         #...........
  25.     if user_input == '+':
  26.         num1 = float(input("відіть цифру: "))
  27.         #веденя 1 цифри  
  28.         num2 = float(input("відіть цифру: "))
  29.         #веденя 2 цифри 
  30.         rvn = (num1 + num2)
  31.         #операция додаваня чифри 1 и уифри 2
  32.         print(num1)
  33.         print("+")
  34.         print(num2)
  35.         print("=")
  36.         print(rvn)
  37.         #для оринтаци користвача 
  38.       #............. 
  39.     elif user_input == '-':
  40.          num1 = float(input('видіть цифру:'))
  41.          num2 = float(input("видіть цифру:")) 
  42.          rvn = (num1 - num2)
  43.          print(num1)
  44.          print("-")
  45.          print(num2)
  46.          print("=")
  47.          print(rvn)
  48. #в цому болоку все таке саме як і в првому тільки знак - зивнив знак +
  49.  #...........
  50.     elif user_input == '*':
  51.          num1 = float(input('видіть цифру:'))
  52.          num2 = float(input("видіть цифру:")) 
  53.          rvn = (num1 * num2)
  54.          print(num1)
  55.          print("*")
  56.          print(num2)
  57.          print("=")
  58.          print(rvn) 
  59.          #.........
  60.     elif user_input == '/':
  61.          num1 = float(input('видіть цифру:'))
  62.          num2 = float(input("видіть цифру:")) 
  63.          rvn = (num1 / num2)
  64.          print(num1)
  65.          print("/")
  66.          print(num2)
  67.          print("=")
  68.          print(rvn)
3 Weeks Ago #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,949
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.
3 Weeks Ago #2

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

Expand|Select|Wrap|Line Numbers
  1. try:
  2.     rvn = (float(num1) / float(num2))
  3.     print(num1)
  4.     print("/")
  5.     print(num2)
  6.     print("=")
  7.     print(rvn)
  8. except ZeroDivisionError:
  9.     print('0')
  10.  
1 Week Ago #3

P: 8
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.
2 Days Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.