How to not let the user divide by 0?

Catz

So here is my code for a very simple program:

import math

valid = True
oper = input('Please input your operation(+, -, *, /): ')
int1 = int(input('Please enter your first number: '))
int2 = int(input('Please enter your second number: '))


while(valid == True):
    if(oper == '/' and int2 == '0'):
        print('Error! Cannot divide by zero!')
        valid = False
    elif(oper == '/' and int2 != '0'):
        print(int1 / int2)
    elif(oper == '+'):
        print(int1 + int2)
    elif(oper == '-'):
        print(int1-int2)
    elif(oper == '*'):
        print(int1 * int2)


    else:
        print('Invalid Operation')

When ever the user inputs in the number 0 for int2, I want the program to print that they can not do that.

Would really appreciate some help getting this program to not let them divide by zero and either ending the program, or taking them back to the start.

Derek Brown

This should do as expected:

import math

while(True):
  oper = input('Please input your operation(+, -, *, /): ')
  int1 = int(input('Please enter your first number: '))
  int2 = int(input('Please enter your second number: '))

  if(oper == '/' and int2 == 0):
      print('Error! Cannot divide by zero!')
  elif(oper == '/'):
      print(int1 / int2)
  elif(oper == '+'):
      print(int1 + int2)
  elif(oper == '-'):
      print(int1-int2)
  elif(oper == '*'):
      print(int1 * int2)
  else:
      print('Invalid Operation')

You will notice a few subtle changes:

  • I moved the loop to outside the input. This way the program loops over and over asking for input.

  • I removed the check for valid. This program will loop forever, asking for new input if the user tries to enter a zero in the denominator (as asked).

  • I removed the quotes from '0'. The code you had before was trying to see if the input was equal to the string 0, which is different than the int 0. This is a small difference (in terms of code) but a very important one in terms of function.

  • I removed the int2 != 0 condition, as it wasn't necessary. oper == '/' and int2 == 0 was already caught, so if oper == '/', then int2 must not be zero.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to return 0 with divide by zero

From Dev

How to let user choose output file name in writecsv

From Dev

How can I let the user load/play their own songs in XNA?

From Dev

Divide by 0 warning

From Dev

How to let user use "sqrt()" + input to find square root of input?

From Dev

How to let stop the user from entering only "()" and outputting only "()" after that

From Dev

How do I let the user cancel a progress notification in Android?

From Dev

How to let the user reorder sections in a UITableView

From Dev

How to let a user choose a picture from the gallery to use in game ,LibGDX

From Dev

How to let two overlapping views to receive the user touch events ios

From Dev

Python: How to let the user input their own solution to the answer key

From Dev

How to eliminate "divide by 0" error in template code

From Dev

How to let user know, an ImageView is clickable

From Dev

How to find OldPassword to let the user change it

From Dev

tvos How to let the user move focus more freely?

From Dev

How to let the user select an input from a finite list?

From Dev

How to grey out a button to let the user know that it is currently disabled in iOS?

From Dev

How do I let the user swipe a CarouselPage when it contains a ListView?

From Dev

How to delete User with User ID and GID 0

From Dev

Divide by 0 warning

From Dev

How to let user know, an ImageView is clickable

From Dev

How to let user pick colormap?

From Dev

How to let the user change the width of a frame

From Dev

How to let a user input a function

From Dev

How to divide an array which has array[0] in 4 values in PHP

From Dev

How can I let the superadmin user and user itself to access a API?

From Dev

How to let user download data to a file

From Dev

How can I let the user fill in an array?

From Dev

How to let the user decide a font for a button?

Related Related

  1. 1

    How to return 0 with divide by zero

  2. 2

    How to let user choose output file name in writecsv

  3. 3

    How can I let the user load/play their own songs in XNA?

  4. 4

    Divide by 0 warning

  5. 5

    How to let user use "sqrt()" + input to find square root of input?

  6. 6

    How to let stop the user from entering only "()" and outputting only "()" after that

  7. 7

    How do I let the user cancel a progress notification in Android?

  8. 8

    How to let the user reorder sections in a UITableView

  9. 9

    How to let a user choose a picture from the gallery to use in game ,LibGDX

  10. 10

    How to let two overlapping views to receive the user touch events ios

  11. 11

    Python: How to let the user input their own solution to the answer key

  12. 12

    How to eliminate "divide by 0" error in template code

  13. 13

    How to let user know, an ImageView is clickable

  14. 14

    How to find OldPassword to let the user change it

  15. 15

    tvos How to let the user move focus more freely?

  16. 16

    How to let the user select an input from a finite list?

  17. 17

    How to grey out a button to let the user know that it is currently disabled in iOS?

  18. 18

    How do I let the user swipe a CarouselPage when it contains a ListView?

  19. 19

    How to delete User with User ID and GID 0

  20. 20

    Divide by 0 warning

  21. 21

    How to let user know, an ImageView is clickable

  22. 22

    How to let user pick colormap?

  23. 23

    How to let the user change the width of a frame

  24. 24

    How to let a user input a function

  25. 25

    How to divide an array which has array[0] in 4 values in PHP

  26. 26

    How can I let the superadmin user and user itself to access a API?

  27. 27

    How to let user download data to a file

  28. 28

    How can I let the user fill in an array?

  29. 29

    How to let the user decide a font for a button?

HotTag

Archive