In this tutorial you will learn:
This is an important function in programming languages almost all programming languages offer some kind of input function to take values from the user.
Input in Python 2.7
Python 2.7 is a bit different from 3.6 and it has a different function for input which is called raw input function. Raw input function takes input from the user and stores it into a variable which can be used while the program is running.
- firstName = raw_input("Your First Name: ")
- lastName = raw_input("Your Last Name: ")
- organization = raw_input ("Your Organization: ")
- print("Your details are: ")
- print("Full Name: ", firstName," ",lastName)
- print("Your organization: ", organization)
Input in Python 3.6
In Python 3.6 the input function was changed from raw input to input. It works in exactly the same way that it takes values from user which are typed and returns a value. In order to get a certain type from it we need to type case the values.
- num1 = int ( input ("Enter first number") )
- num2 = int ( input ("Enter second number") )
- sum = num1 + num2
- print("Addition of two number is: ", sum)
- num1 = float ( input ("Enter first number") )
- num2 = float ( input ("Enter second number") )
- subtraction = num1 - num2
- print("Subtraction of two floating point numbersis: ", subtraction)
Making a simple Calculator
Now let’s make a simple calculator using Python. It will have functionality to add, subtract, multiply and divide. In order to develop this program we need to think logically and write everything step by step. So let’s list down the steps that we need to make a calculator and then write the code along with it.
1. The first step is to take input from the user asking which operation needs to be performed and then ask for the first and second numbers.
- selection = int(input("\n\nEnter selection ( 1 Multiply \n 2 Divide \n 3 Subtract \n 4 Add ): "))
- num1 = float(input("Enter first number: "))
- num2 = float(input("Enter second number: "))
2. Second step is to write the functions for performing the arithmetic operations such as multiply, divide, subtract and add.
- def multiply(x, y):
- return x * ys
- def divide(x, y):
- return x / y
- def subtract(x, y):
- return x - y
- def add(x, y):
- return x + y
3. Third step is to perform the operation based on the selection and show the result.
- if selection == 1:
- print(num1,"*",num2,": ", multiply(num1,num2))
- elif selection == 2:
- print(num1,"/",num2,": ", divide(num1,num2))
- elif selection == 3:
- print(num1,"+",num2,": ", add(num1,num2))
- elif selection == 4:
- print(num1,"-",num2,": ", subtract(num1,num2))
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.