Home > Software design >  How to find the quotient of two numbers in a list - python
How to find the quotient of two numbers in a list - python

Time:02-19

I am a begineer in python and i am trying to create a basic calculator.So what i did was to ask the user for input and put it in a list called operations.

operations = []

I performed all the operations sucessfully except with division.Beacause i couldn't preform the division operation in the list ,I created two variabes then place all of them in the list operations.Since they where input from users i could not use the append fuction.So i did this:

a = int()
b = int()
operation = [a,b]
divide_result=a//b
print(f'Your result is{divide_result}')

I did not want to declare a function for this because i was going to use it once. So after this code i got this error:

divide_result=a//b ZeroDivisionError: integer division or modulo by zero

CodePudding user response:

You initialized your variables to 0 by calling int()

Than you tried to divide by zero.

What you probably wanted to do was:

a = int(input("Enter dividend:"))
b = int(input("Enter divisor:"))

divide_result = a // b
print(f'Your result is{divide_result}')

CodePudding user response:

It is possible that somehow your variable b got set to 0.

Here's my code and it works

calc.py

import sys

def main():
    a = input("please enter integer a: ")
    b = input("please enter integer b: ")

    a = int(a)
    b = int(b)

    if b == 0:
        print("cannot enter 0 for b. divide by 0 error")
        sys.exit(1)

    result = a/b
    print(f"{a}/{b} = {result}")


if __name__ == "__main__":
    main()

python3 calc.py
please enter integer a: 10
please enter integer b: 20
10/20 = 0.5
  • Related