Saad_Ali786
Saad_Ali786

Reputation: 13

Making a Calculator which take input from user until user enter 0 but not working correctly

I am a newbie in python and trying to make a calculator but no getting how to make it I am making a Calculator which will take input from the user until the user enters 0 and then do the operations but I am stuck here if anyone can help me doing this work I will be very thankful to him/her.

num = None

# Asking Users for the Specific Operations

print(("1. For Addition \n 2. For Subtraction. \n 3. For Multiplication. \n 4. For Division \n 5.For Exit"))

options = int(input("Enter Your Choice: "))

# For Addition or Option 1

if options == 1:
    total = 0
    while(num != 0):
        try:
            num = float(input("(Enter \'0'\ When Complete.) Enter Number "))
        except:
            print("Error, Enter Valid Number")
            continue
        total = total + num
    print("Your Calculated Number is: {} ".format(total))

# For Subtraction or Option 2

elif options == 2:
    total = 0
    while (num != 0):
        try:
            num = float(input("(Enter \'0'\ When Complete.) Enter Number "))
        except:
            print("Error, Enter Valid Number")
            continue
        total = total - num
    print("Your Calculated Value is: {}".format(total))

# Multiplication for Option 3

elif options == 3:
    total = 1
    while (num != 0):
        try:
            num = float(input("(Enter \'0'\ When Complete.) Enter Number "))
        except:
            print("Error, Enter Valid Number")
            continue
        total = total * num
    print("Your Calculated Value is: {}".format(total))

# Division for Option 4

elif options == 4:
    total = 1
    while (num != 0):
        try:
            num = float(input("(Enter \'0'\ When Complete.) Enter Number "))
        except:
            print("Error, Enter Valid Number")
            continue
        total = total / num
    print("Your Calculated Value is: {}".format(total))

# When User Wants to Exit

else:
    print("Thank You for Using the Calculator")

Upvotes: 1

Views: 857

Answers (3)

Stuart
Stuart

Reputation: 9858

Here is a better approach using itertools.reduce. Instead of repeating the same code for inputting a number multiple times, put it into a function. This will also help avoid the errors in your code and clarify the logic. A second generator function can be used to get the series of values until the user enters zero.

from functools import reduce
import operator

def input_number():
    while True:
        try:
            return float(input("(Enter '0' When Complete.) Enter Number "))
        except:
            print("Error, Enter Valid Number")
        
def input_series():
    while True:
        n = input_number()
        if n == 0:
            return
        yield n

operations = {
      1: operator.add,
      2: operator.sub,
      3: operator.mul,
      4: operator.truediv
      }
# Asking Users for the Specific Operations
print(("1. For Addition \n 2. For Subtraction. \n 3. For Multiplication. \n 4. For Division \n 5.For Exit"))
option = int(input("Enter Your Choice: "))

# For Addition or Option 1

if option == 5:
    print("Thank You for Using the Calculator")
else:
    total = reduce(operations[option], input_series())
    print("Your Calculated Value is: {}".format(total))
    

Upvotes: 1

elbashmubarmeg
elbashmubarmeg

Reputation: 386

The problem with subtraction is that the variable total is not initialized.

The problem with multiplication and division is that when the user inputs "0", the variable total is multiplied or divided by zero before it is checked in the while statement. what I would normally do is this:

elif options == 3:
total = 1
while True:
    try:
        num = float(input("(Enter '0' When Complete.) Enter Number "))  # No need to escape single quotes when your string uses double quotes 
        if num == 0:
            break
    except ValueError:
        print("Error, Enter Valid Number")
        continue
    total = total * num
print("Your Calculated Value is: {}".format(total))

However, if you wanted a quick fix, you can have the user input 1 instead of 0 for multiplication and division:

elif options == 4:
total = 1
while (num != 1): 
    try:
        num = float(input("(Enter '1' When Complete.) Enter Number "))
    except:
        print("Error, Enter Valid Number")
        continue
    total = total / num
print("Your Calculated Value is: {}".format(total))

Edit: If you want division to work the way you specified, you could do something like this:

elif options == 2:
    total = 1
    try:
        first_number = float(input("(Enter '0' When Complete.) Enter Number "))
        if first_number == 0:
            print("Your Calculated Value is: 0")
            exit()
    except ValueError:
        print("Error, Enter Valid Number")
        continue

    total = 1
    while True:
        try:
            num = float(input("(Enter '0' When Complete.) Enter Number "))
            if num == 0:
                break
        except ValueError:
            print("Error, Enter Valid Number")
            continue
        total = total * num
    print("Your Calculated Value is: {}".format(total + first_number))

Upvotes: 0

MarianD
MarianD

Reputation: 14161

Instead of

elif options == 2:
    total = 0
    while (num != 0):
        try:
            num = float(input("(Enter \'0'\ When Complete.) Enter Number "))
        except:
            print("Error, Enter Valid Number")
            continue
        total = total - num

use (the changes are only in the 2nd line and in the last one)

elif options == 2:
    total = None
    while (num != 0):
        try:
            num = float(input("(Enter \'0'\ When Complete.) Enter Number "))
        except:
            print("Error, Enter Valid Number")
            continue
        total = total - num if total is not None else num

The same method you may use for the elif options == 4: branch.

Upvotes: 0

Related Questions