Python Forum
Beginner Help request: Calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Help request: Calculator
#1
Hello,

I am just starting to get my feet wet with Python and would like to ask for some insight as to why this script goes directly to the "else" line.

The code is as follows:

# adding two numbers
def add(x, y):
    return x + y

#subtracting two numbers
def subtract(x, y):
    return x - y

#multiplies two numbers
def multiply(x, y):
    return x * y

#divide two numbers
def divide(x, y):
    return x / y

print("Select operation")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

#take input from user
choice = input("Enter choice(1/2/3/4):")


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
    print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
    print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
    print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
    print(num1,"/",num2,"=", divide(num1,num2))

else:
    print("Invalid input")
Cheers,
J
Reply


Messages In This Thread
Beginner Help request: Calculator - by JinLee - Sep-19-2017, 06:55 AM
RE: Beginner Help request: Calculator - by ocpaul20 - Sep-19-2017, 07:14 AM
RE: Beginner Help request: Calculator - by Sagar - Sep-19-2017, 07:41 AM
RE: Beginner Help request: Calculator - by buran - Sep-19-2017, 08:02 AM
RE: Beginner Help request: Calculator - by JinLee - Sep-20-2017, 12:40 AM
RE: Beginner Help request: Calculator - by nilamo - Sep-20-2017, 08:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how can I correct the Bad Request error on my curl request tomtom 8 5,140 Oct-03-2021, 06:32 AM
Last Post: tomtom
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,979 Jun-18-2020, 08:07 AM
Last Post: buran
  BEGINNER: My calculator doesnt work iskov 5 3,260 Oct-09-2019, 07:45 AM
Last Post: buran
  Beginner. Calculator problem ¯\_(ツ)_/¯ stykus1992 0 2,381 Feb-15-2018, 11:01 AM
Last Post: stykus1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020