반응형
Basic Python calculator
I am new to Python. I tried to make a simple calculator, but what is the problem?
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def div(num1, num2):
return num1/num2
def multi(num1,num2):
return num1*num2
def main():
operation = input("What do you want to do?(+, -, *, or /):")
if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
print("Your input is invalid. Please enter a valid input.")
else:
num1 = float(input("Enter value for num1: "))
num2 = float(input("Enter value for num2: "))
if (operation == "+"):
print(add(num1, num2))
elif (operation == "-"):
print(subtract(num1, num2))
elif (operation == "*"):
print(multi(num1,num2))
elif (operation == "/"):
print(div(num1,num2))
main()
Simple calculator with very Easy steps in python. You are not calling the functions inside main.
# Defining Functions
def sum (x,y):
return x+y
def div (x,y):
return x/y
def sub (x,y):
return x-y
def multi (x,y):
return x*y
# getting Input for the operation to be performed
choice =input("Plesae enter choice:)\n'div' for Divide \n'multi' for Multiply\n'sub' for Subtraction\n'sum' for Addition\n")
#Performing opration According to the input of user
if(choice == "sum"):#condition
x = int(input("Please enter number 1 :"))
y = int(input("Please enter number 1 :"))
ans = sum(x,y)
print(f"your Answer is {x} + {y} = {ans} ")
elif(choice == "div"): #condition
x = int(input("Please enter number 1 :"))
y = int(input("Please enter number 1 :"))
ans = div(x,y)
print(f"your Answer is {x} + {y} = {ans} ")
elif(choice == "sub"):#condition
x = int(input("Please enter number 1 :"))
y = int(input("Please enter number 1 :"))
ans = sub(x,y)
print(f"your Answer is {x} + {y} = {ans} ")
elif(choice == "multi"): #condition
x = int(input("Please enter number 1 :"))
y = int(input("Please enter number 1 :"))
ans = multi(x,y)
print(f"your Answer is {x} + {y} = {ans} ")
else:
print("Please Enter a Valid Number")
You call main from inside itself. Set this outside the function like this:
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def div(num1, num2):
return num1/num2
def multi(num1,num2):
return num1*num2
def main():
operation = input("What do you want to do?(+, -, *, or /):")
if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
print("Your input is invalid. Please enter a valid input.")
else:
num1 = float(input("Enter value for num1: "))
num2 = float(input("Enter value for num2: "))
if (operation == "+"):
print(add(num1, num2))
elif (operation == "-"):
print(subtract(num1, num2))
elif (operation == "*"):
print(multi(num1,num2))
elif (operation == "/"):
print(div(num1,num2))
main() # Added main outside the function
Your main() has a Tab behind (before) it. It didn't run for me at first.
The other things seem fine to me.
You could also have it in a loop if you want to make it nicer.
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def div(num1, num2):
return num1/num2
def multi(num1,num2):
return num1*num2
def main():
operation = input("What do you want to do?(+, -, *, or /):")
if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
print("Your input is invalid. Please enter a valid input.")
else:
num1 = float(input("Enter value for num1: "))
num2 = float(input("Enter value for num2: "))
if (operation == "+"):
print(add(num1, num2))
elif (operation == "-"):
print(subtract(num1, num2))
elif (operation == "*"):
print(multi(num1,num2))
elif (operation == "/"):
print(div(num1,num2))
if __name__ == '__main__':
while(True):
main()
if input('If you are done with calculating, type q: ') == 'q':
break
반응형
'개발하자' 카테고리의 다른 글
FastAPI를 이용하여 엑셀 파일을 반납하고 다운로드하는 방법은? (0) | 2023.09.13 |
---|---|
ECS Fargate 및 Terraform으로 개인 도커 레지스트리 액세스 (0) | 2023.09.12 |
Typescript에 해당하는 typedef (0) | 2023.09.11 |
Svelte 구성 요소를 트리거/강제 업데이트하는 방법 (0) | 2023.09.11 |
젠킨스를 사용하여 kubernetes 배포 업데이트 (0) | 2023.09.09 |