-
Notifications
You must be signed in to change notification settings - Fork 0
/
mycalc.py
58 lines (47 loc) · 2.79 KB
/
mycalc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import sys
while True:
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
def add (x, y):
return x + y
def subtract (x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x ,y):
return x / y
choice = input("Enter choice(1/2/3/4): ")
if choice > '4.': # (REFER TO TAG 1) : Seeing if this would work ( something to compare to )
print ("Invalid Input")
answer = input('Run again? (y/n): ')
if answer == ('y'):
continue
if answer == ('n'):
sys.exit()
num1 = float((input("Enter first number: "))) # Got rid of float() before input UPDATE: you NEED to add float to change it to a decimal number!, earlier without float it would do for example, 12+12 without float would be 1212 but with float it would be 24.0
num2 = float((input("Enter second number: "))) # Got rid of float() before input
if choice == "1": # Changed single speech mark to double.
print(num1,"+",num2,"=", add(num1,num2))
elif choice == "2": # Changed single speech mark to double.
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == "3": # Changed single speech mark to double.
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == "4": # Changed single speech mark to double.
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid Input")
#Thinking of making it so that the user can do: add num + num instead of having to select add etc..
#added the . infront of the 4 at line 26 to change it to a float to accept higher number, and also accept numbers that are decimals (floats).
#indentation is very important had to change the entire piece of code.
#DONE WHILE TRUE has to be at the top to start the questions again: refer to my ques: https://stackoverflow.com/questions/58790686/trying-to-make-a-loop-that-stops-on-user-input
# TAG 1 Right now trying to make it so that if the choice is more than 4 it will say invalid input.
# TAG 1 DONE!!!, found out you need to put it right after the (Choice) variable, right now it does not stop the script after the user has done an invalid input.
# TAG 1 Got the method for while from this website: https://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements
# Python operands https://www.tutorialspoint.com/python/comparison_operators_example.htm
# search what def means and what elif means.
# Used this for the calc code: https://www.programiz.com/python-programming/examples/calculator also (Calculator.py has the actual file)
# Used this website for floating point https://www.google.com/search?client=firefox-b-d&q=how+does+float+input+mean+in+python