'how to make a percentage calculator using python
I want to develop a calculator that does simple math using Python with user input for one of my projects for high school, but I can't figure out how to write the input for percentage. So I was hoping that someone could assist me.
Here is my code---
import math
print("What kind of operation do you want to perform? ")
print("a. Add")
print("b. Subtract")
print("c. Multiply")
print("d. Divide")
print("e. Square Root")
print("f. Percentage")
print("Please select by choosing a, b, c, d, e, or f.")
def add(ip1, ip2):
return ip1 + ip2
def subtract(ip1, ip2):
return ip1 - ip2
def multiply(ip1, ip2):
return ip1 * ip2
def divide(ip1, ip2):
return ip1 / ip2
def squareroot(ip1):
return math.sqrt(ip1)
def percentage(ip1)
return
def main():
option = (input("Choose the sort of calculation you want to perform: "))
ip1 = int(input("Start by entering the first number you'd want to calculate: "))
ip2 = int(input("Fill in the second number you want to calculate: "))
if option == "a":
print(ip1, "+", ip2, "=", add(ip1, ip2))
elif option == "b":
print(ip1, "-", ip2, "=", subtract(ip1, ip2))
elif option == "c":
print(ip1, "*", ip2, "=", multiply(ip1, ip2))
elif option == "d":
print(ip1, "/", ip2, "=", divide(ip1, ip2))
elif option == "e":
print(squareroot(ip1))
else:
print("Wrong Input")
main()
Solution 1:[1]
What I understand from your question is that you want to calculate what percentage of the 2nd input is the 1st input. If that is the case, then you could do something like :-
def percentage(ip1, ip2):
return ip1 / ip2 * 100
What this basically does, is that it divides the first input by the second input and multiplies the outcome by hundred. This will output what percentage the first input is of the second input.
Eg:-
inp1 = 5, inp2 = 10
process: 5 / 10 * 100
output: 50(%)
P.S. - You also need to add the elif
clause for option == f
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |