Invalid syntax error on "def" in my coding. - python

Here is my code: I'm not sure what the problem is. It says invalid syntax on "def sub". I've looked everywhere and as far as I can tell, it is properly formatted for python 3
def add():
print ("Enter the two numbers to Add")
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A + B
def sub():
print ("Enter the two numbers to Subtract")
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A - B
def mul():
print ("Enter the two numbers to Multiply")
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A * B
def div():
print ("Enter the two number to Divide")
A=float(raw_input("Enter A: "))
B=float(raw_input("Enter B: "))
return A / B
print ("1: ADD")
print ("2: SUBTRACT")
print ("3: MULTIPLY")
print ("4: DIVIDE")
print ("0: QUIT")
while True:
CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION "))
if CHOICE == 1:
print ('ADD TWO NUMBERS:')
print add()
elif CHOICE == 2:
print ('SUBTRACT TWO NUMBERS')
print sub()
elif CHOICE == 3:
print ('MULTIPLY TWO NUMBERS')
print mul()
elif CHOICE == 4:
print ("DIVIDE TWO NUMBERS")
print div()
elif CHOICE == 0:
exit()
else:
print ("The value Enter value from 1-4")

All the prints like:
print add()
are missing the parenthesis. They should be:
print(add())
Same for all the prints

Related

Menu for python calculator

I have created a calculator in python that can calculate the addition, subtraction, multiplication, division or modulus of two integers. What equation is executed is based on which number is type in from the menu, and I would like the user to be able to go back to the menu after an equation after being asked whether or not to "continue?". Would appreciate any help
print("MENU")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulous")
menu = input("Enter your choice: ")
if int(menu) == 1:
def additon(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
return(number1 + number2)
answer1 = additon()
print("Result:", answer1)
if int(menu) == 2:
def subtraction(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
return(number1 - number2)
answer2 = subtraction()
print("Result: ", answer2)
if int(menu) == 3:
def multiplication(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
return(number1 * number2)
answer3 = multiplication()
print("Result: ", answer3)
if int(menu) == 4:
def division(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
return(number1 / number2)
answer4 = division()
print("Result: ", answer4)
if int(menu) == 5:
def modulus(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
return(number1 % number2)
answer5 = modulus()
print("Result: ", answer5)
if int(menu) != 1 or 2 or 3 or 4 or 5:
print("Not a valid option")
First define your functions once at the top of the program, you are
currently defining functions (eg. addition) inside if statements
put all your main code in a 'forever' (while True:) loop so that each time through the loop it prints out the menu, accepts input, and displays the results then returns to the top of the loop to do it all over again
when you read user input, before converting it to an integer, add code to check if the user typed something like 'q' (for quit) and if they did then use break to take you out of the 'forver' loop and end the program
here's a skeleton version:
def print_menu():
"""print the menu as show in your code"""
def addition():
"""your addition function"""
...
def subtraction():
"""your subtraction function"""
...
# etc
while True: # 'forever' loop
print_menu()
resp = input("your choice? ")
if resp == 'q': # user wants to quit
break # so break out of forever loop
resp = int(resp) # change your resp to an integer
if resp == 1:
answer = addition()
elif resp == 2:
answer = subtraction()
elif resp == 3:
answer = multiplication()
elif resp == 4:
answer = division()
else:
print("unknown choice, try again")
continue # go back to top of loop
print("The Answer Is", answer)
An answer has already been accepted for this but I'll throw this out there anyway.
This kind of exercise is ideally suited to a table-driven approach.
The operations are trivial so rather than define discrete functions a lambda will suffice
Define a dictionary where we look up and validate the user's option.
Get the user input and carry out sanity checks.
We end up with just 2 conditional checks - one of which is merely concerned with quitting the [potentially] infinite loop.
CONTROL = {'1': lambda x, y: x + y,
'2': lambda x, y: x - y,
'3': lambda x, y: x * y,
'4': lambda x, y: x / y,
'5': lambda x, y: x % y}
while True:
print("MENU")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
option = input('Select an option or q to quit: ')
if option and option in 'qQ':
break
if option in CONTROL:
x = input('Input X: ')
y = input('Input Y: ')
try:
result = CONTROL[option](int(x), int(y))
print(f'Result={result}')
except ValueError:
print('Integer values only please')
except ZeroDivisionError:
print("Can't divide by zero")
else:
print('Invalid option')
print()
Wrap your code in a while loop. The following code should work.
def additon(number1, number2):
return (number1 + number2)
def subtraction(number1, number2):
return(number1 - number2)
def multiplication(number1, number2):
return(number1 * number2)
def division(number1, number2):
return(number1 / number2)
def modulus(number1, number2):
return(number1 % number2)
x = True
while (x):
print("MENU")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulous")
menu = input("Enter your choice: ")
if int(menu) == 1:
answer1 = additon(number1=int(input("Enter first number: ")),
number2=int(input("Enter second number: ")))
print("Result:", answer1)
elif int(menu) == 2:
answer2 = subtraction(number1=int(input("Enter first number: ")),
number2=int(input("Enter second number: ")))
print("Result: ", answer2)
elif int(menu) == 3:
answer3 = multiplication(number1=int(input("Enter first number: ")),
number2=int(input("Enter second number: ")))
print("Result: ", answer3)
elif int(menu) == 4:
answer4 = division(number1=int(input("Enter first number: ")),
number2=int(input("Enter second number: ")))
print("Result: ", answer4)
elif int(menu) == 5:
answer5 = modulus(number1=int(input("Enter first number: ")),
number2=int(input("Enter second number: ")))
print("Result: ", answer5)
elif int(menu) < 1 or int(menu) > 5:
print("Not a valid option")
go_again = input("Would you like to go again (y/n): ")
if (go_again.lower()=="n"):
x = False
else:
continue

My function keeps producing the word "none" in the output

I am in a beginners computer science class, and for my final I have to create a menu that includes all of my previous coding assignments using only the basic techniques that we have been taught. I have created a code that works, but the problem is my code also produces the word "none" in the output.
def options():
print ("Pick from the list")
print ("1. Grade Conversion")
print ("2. Temperature Conversion")
print ("3. Currency Conversion")
print ("4. Sum and Average")
print ("5. Heads or Tails Guessing Game")
print ("6. Fibonacci Sequence")
print ("7. Factorials")
print ("8. Multiplication Table")
print ("9. Guess the Number Game")
print ("10. Calculator")
print ("11. Exit")
def Student_A_Gets_A():
print ("Student A gets Grade A")
print (Score)
def Student_B_Gets_B():
print ("Student B gets Grade B")
print (Score)
def Student_C_Gets_C():
print ("Student C gets Grade C")
print (Score)
def Student_D_Gets_D():
print ("Student D gets Grade D")
print (Score)
def Student_F_Gets_F():
print ("Student F gets Grade F")
print (Score)
def Celsius():
Temperature = int(input ("Please enter Temp. in Celsius: "))
print ("Fahrenheit = ", (Temperature*(9/5))+32)
def Fahrenheit():
Temperature = int(input ("Please enter Temp. in Fahrenheit: "))
print ("Celsius = ", (Temperature-32)*(5/9))
def Currency_Conversion():
print ("Pick from the list")
print ("1. Dollar to Euro")
print ("2. Dollar to Peso")
print ("3. Euro to Dollar")
print ("4. Peso to Dollar")
print ("5. Euro to Peso")
print ("6. Peso to Euro")
def Dollar_to_Euro():
print ("You have selected 1 to convert Dollar to Euro")
Amount = int(input("Please enter the amount in dollar(s): "))
print ("$", Amount, " = ", "€",(Amount)*(0.813654))
def Dollar_to_Peso():
print ("You have selected 2 to convert Dollar to Peso")
Amount = int(input("Please enter the amount in dollar(s): "))
print ("$", Amount, " = ", "Mex$",(Amount)*(18.695653))
def Euro_to_Dollar():
print ("You have selected 3 to convert Euro to Dollar")
Amount = int(input("Please enter the amount in euro(s): "))
print ("€", Amount, " = ", "$",(Amount)/(0.813654))
def Peso_to_Dollar():
print ("You have selected 4 to convert Peso to Dollar")
Amount = int(input("Please enter the amount in peso(s): "))
print ("Mex$", Amount, " = ", "$",(Amount)/(18.695653))
def Euro_to_Peso():
print ("You have selected 5 to convert Euro to Peso")
Amount = int(input("Please enter the amount in euro(s): "))
print ("€", Amount, " = ", "Mex$",(Amount)*(22.98))
def Peso_to_Euro():
print ("You have selected 6 to convert Peso to Euro")
Amount = int(input("Please enter the amount in peso(s): "))
print ("$", Amount, " = ", "€",(Amount)/(22.98))
def options2 ():
print ("Select Operation")
print ("1. Add 2 numbers")
print ("2. Subtract 2 numbers")
print ("3. Multiply 2 numbers")
print ("4. Divide 2 numbers")
print ("5. Guessing Game")
print (options())
answer = int(input("Enter a choice from 1 through 10: "))
while (1):
if answer < 1 and answer > 11:
print ("Sorry, that is not an option. Enter a number from 1 through 11")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 1:
Score = int(input("Please enter Score: "))
if Score >= 90 and Score <= 100:
print (Student_A_Gets_A())
print (" ")
print (options ())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 80 and Score <= 89:
print (Student_B_Gets_B())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 70 and Score <= 79:
print (Student_C_Gets_C())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 60 and Score <= 69:
print (Student_D_Gets_D())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 50 and Score <= 59:
print (Student_F_Gets_F())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error: Score must be between 100 and 50 for grades A through F")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 2:
Celsius_or_Fahrenheit = int(input ("Please enter 1 for Celsius or 2 for Fahrenheit: "))
if Celsius_or_Fahrenheit == 1:
print (Celsius())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Celsius_or_Fahrenheit == 2:
print (Fahrenheit())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, must choose 1 for Celsius or 2 for Fahrenheit")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 3:
print (Currency_Conversion())
Currency_Conversion_answer = int(input("Enter a choice from 1 through 6: "))
if Currency_Conversion_answer == 1:
print (Dollar_to_Euro())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 2:
print (Dollar_to_Peso())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 3:
print (Euro_to_Dollar())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 4:
print (Peso_to_Dollar())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 5:
print (Euro_to_Peso())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 6:
print (Peso_to_Euro())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, number not within bounds")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 4:
number = int(input("How many numbers would you like to add: "))
counter = 0
average = 0
for j in range(1,number+1):
if j %10 == 0:
print (j,",")
elif j == number:
print (j)
else:
print (j,"," ,end="")
for i in range(1,number + 1):
counter += i
average = counter/number
print ("sum = ", counter)
print ("average = ", average)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 5:
import random
rand = random.randint(1,2)
guess = int(input("Guess Heads(1) OR Tails(2): "))
if guess is 1 or guess is 2 and guess is rand:
print ("You win")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif guess is 1 or guess is 2 and guess is not rand:
print ("You lose")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, number must be 1 or 2")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 6:
fib = int(input("How many Fibonacci numbers shall I generate? "))
x = 1
y = 1
print ("Fibonacci sequence up to", fib)
for i in range (1):
for i in range (1,fib+1):
x = y - x
y = x + y
if i %10 == 0 and i is not fib:
print (x, ", ")
elif i == fib:
print (x)
else:
print (x, ", ", end="")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 7:
Number = int(input("Factorial of what number do you want? Number must be less than or equal to 20. "))
if Number > 20:
print ("Error")
else:
Factorial = 1
for i in range(1,Number + 1):
print (i,)
Factorial = Factorial * i
print("Factorial of", Number, " is ",Factorial)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 8:
num = int(input("Enter multiplication table you want to see: "))
maximum = int(input("Enter max you want your table to go to: "))
counter = 0
print ("The table of", num)
for i in range (0,maximum):
counter = i + 1
print (counter, "*", num, "=", counter*num)
print ("Done counting...")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 9:
import random
number = int(input("Guess any number 1 to 10? "))
guess = random.randint(1,10)
acc = 0
while guess > 0:
if guess < number:
print ("Too High")
number = int(input("Guess any number 1 to 10? "))
acc+=1
elif guess > number:
print ("Too Low")
number = int(input("Guess any number 1 to 10? "))
acc+=1
elif guess == number:
acc+=1
break
print ("And it only took you", acc, "tries")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 10:
import random
rand = random.randint(1,4)
print (options2())
answer = int(input("Enter a choice from 1 through 5: "))
if answer > 5 and answer < 1:
print ("Sorry, that is not an option")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 1:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"+",y,"=",x+y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 2:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"-",y,"=",x-y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 3:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"*",y,"=",x*y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 4:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
if y == 0:
print ("Error, denominator cannot be zero")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print (x,"/",y,"=",x/y)
elif answer == 5:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
if rand == 1:
print (x,"+",y,"=",x+y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 2:
print (x,"-",y,"=",x-y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 3:
print (x,"*",y,"=",x*y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 4:
print (x,"/",y,"=",x/y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, must choose from the aforementioned selections")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 11:
break
print ("You have exited")
Printing return values from functions, as #Patrick Haugh indicates. For example, line 82:
print (options())
runs the function options(), which prints out the alternatives, and then prints the return value from options(), which is None as you don't specify any return value. Instead code line 82 as:
options()
You need to correct this on a lot of places, and then for other functions as well, e.g. Celsius() etc.

don't matter what the input is it won't get to the "else if" statement [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
my output is the menu() function prints only all over again and again, don't matter what the input is it won't get to the "else if" statement #Python...........................................................................
def menu():
#print what options you have
print ("Welcome to calculator.py")
print ("your options are:")
print (" ")
print ("1) Addition")
print ("2) Subtraction")
print ("3) Multiplication")
print ("4) Division")
print ("5) exposion")
print ("6) Quit calculator.py")
print (" ")
return input ("Choose your option: ")
# this adds two numbers given
def add(a,b):
print (a, "+", b, "=", a + b)
# this subtracts two numbers given
def sub(a,b):
print (b, "-", a, "=", b - a)
# this multiplies two numbers given
def mul(a,b):
print (a, "*", b, "=", a * b)
# this divides two numbers given
def div(a,b):
print (a, "/", b, "=", a / b)
# HERE IS MY CODE
def dinami(base, exponent):
if y == 1:
print (base, "*" ,power,"(",base,",", exponent," - 1) = ",base)
if y != 1:
print (base, "*" ,power,"(",base,",", exponent," - 1) = ",base * power(base, exponent - 1))
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add this: "),input("to this: "))
print ("hi")
elif choice == 2:
sub(input("Subtract this: "),input("from this: "))
elif choice == 3:
mul(input("Multiply this: "),input("by this: "))
elif choice == 4:
div(input("Divide this: "),input("by this: "))
elif choice == 5:
dinami(input("expone this: "),input("to this: "))
elif choice == 6:
loop = 0
print ("Thankyou for using calculator.py!")
input() returns a string, so if you're comparing int with string i.e '1' == 1, which always returns False
https://docs.python.org/3/library/functions.html#input

what's this indentationError?

print("Welcome to calculator.py")
print ("your options are :")
print ("")
print ("1) Addition")
print ("2)Subtraction")
print ("3) Multiplication")
print ("4)Division")
print ("Choose your option")
#this adds 2 numbers given
def add (a,b):
print(a, "+" ,b, "=" ,a+b)
#this subtracts 2 numbers given
def sub (a,b):
print(a, "-" ,b, "=" ,b-a)
#this multiplies 2 numbers given
def mul(a,b):
print(a, "*" ,b, "=" ,a*b)
#this divides 2 numbers given
def div(a,b):
def div(a, "/" ,b, "=" ,a/b)
#NOW THE PROGRAM REALLY STARTS ,AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add (input("Add this: "),input ("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input ("from this: "))
elif choice == 3:
mul (input("Multiply this: "),input ("by this: "))
elif choice == 4:
div(input("Divide this: "),input ("by this: "))
elif choice ==5:
loop = 0
print ("Thank you for using calculator.py")
this is my code and the error is:
print(a, "+" ,b, "=" ,a+b)
^
IndentationError: expected an indented block
Process finished with exit code 1
and many others can anyone help me find all the errors???
can anyone help me???
In python, there are identation rules to know how the program should execute commands. This is the way python recognize when a function, a conditional statement or for, while statements start and end. In your case, for example, the elif statement should be at the same "level" as your initial if statement start:
print("Welcome to calculator.py")
print ("your options are :")
print ("")
print ("1) Addition")
print ("2)Subtraction")
print ("3) Multiplication")
print ("4)Division")
print ("Choose your option")
#this adds 2 numbers given
def add (a,b):
print(a, "+" ,b, "=" ,a+b)
#this subtracts 2 numbers given
def sub (a,b):
print(a, "-" ,b, "=" ,b-a)
#this multiplies 2 numbers given
def mul(a,b):
print(a, "*" ,b, "=" ,a*b)
#this divides 2 numbers given
def div(a,b):
def div(a, "/" ,b, "=" ,a/b)
#NOW THE PROGRAM REALLY STARTS ,AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add (input("Add this: "),input ("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input ("from this: "))
elif choice == 3:
mul (input("Multiply this: "),input ("by this: "))
elif choice == 4:
div(input("Divide this: "),input ("by this: "))
elif choice ==5:
loop = 0
In python blocks are defined by indentation. After defining function you should have a indentation(tab).
Pretty much all of the indentation is wrong, not just that one place:
print("Welcome to calculator.py")
print("your options are :")
print("")
print("1) Addition")
print("2)Subtraction")
print("3) Multiplication")
print("4)Division")
print("Choose your option")
#this adds 2 numbers given
def add(a,b):
print(a, "+", b, "=" , a+b)
#this subtracts 2 numbers given
def sub(a,b):
print(a, "-", b, "=" , b-a)
#this multiplies 2 numbers given
def mul(a,b):
print(a, "*", b, "=" , a*b)
#this divides 2 numbers given
def div(a,b):
print(a, "/", b, "=", a/b)
#NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add this: "), input("to this: "))
elif choice == 2:
sub(input("Subtract this: "), input("from this: "))
elif choice == 3:
mul(input("Multiply this: "), input("by this: "))
elif choice == 4:
div(input("Divide this: "), input("by this: "))
elif choice ==5:
loop = 0
print ("Thank you for using calculator.py")

I am coding a calculator and need help allowing user inputs to be floats

Okay, this is my code, it is in python 3.4.3 and I do not know how I would go about allowing user inputs to be floats. Any help would be greatly appreciated.
It is a calculator and works perfectly but it does not allow user inputs to be floats(have decimal places) and a lot of calculations take place with inputs of decimal numbers so it kinda needs it. Thanks if you take the time to read that!
import time
def cls(): print ("\n"*100)
def add():
cls()
print("you have selected addition")
a = input("Enter your first number: ")
while a.isdigit() == False:
print("Enter a numerical interger")
a = input("Enter your first number: ")
if a.isdigit() == True:
a = int(a)
b = input("Enter your second number: ")
while b.isdigit() == False:
print("Enter a numberical interger")
b = input ("enter your second number: ")
if b.isdigit() == True:
b = int(b)
print ("\n")
print ("ANSWER:",a,"+",b,"=",a+b)
print ("\n")
def sub():
cls()
print("you have selected subtraction")
a = input("Enter your first number: ")
while a.isdigit() == False:
print("Enter a numerical interger")
a = input("Enter your first number: ")
if a.isdigit() == True:
a = int(a)
b = input("Enter your second number: ")
while b.isdigit() == False:
print("enter a numerical interger")
b = input("Enter your second number: ")
if b.isdigit() == True:
b = int(b)
print("\n")
print ("ANSWER:",a,"-",b,"=",a-b)
print("\n")
def multi():
cls()
print ("you have selected multiplication")
a = input("Enter your first number: ")
while a.isdigit() == False:
print("Enter a numerical interger")
a = input("Enter your first number: ")
if a.isdigit() == True:
a = int(a)
b = input("Enter your second number: ")
while b.isdigit() == False:
print("enter a numerical interger")
b = input("Enter your second number: ")
if b.isdigit() == True:
b = int(b)
print("\n")
print("ANSWER:",a,"*",b,"=",a*b)
print("\n")
def divide():
cls()
print ("you have selected division")
a = input("Enter your first number: ")
while a.isdigit() == False:
print("Enter a numerical interger")
a = input("Enter your first number: ")
if a.isdigit() == True:
a = int(a)
b = input("Enter your second number: ")
while b.isdigit() == False:
print("enter a numerical interger")
b = input("Enter your second number: ")
if b.isdigit() == True:
b = int(b)
c = (a/b)
if a%b ==0 :
print("\n")
print ("ANSWER:",a,"/",b,"=",int(c))
print("\n")
else :
print("\n")
print ("ANSWER:",a,"/",b,"=",float(c))
print("\n")
def indice():
cls()
print ("you have selected indice multiplication")
a = input("Enter your first number: ")
while a.isdigit() == False:
print("Enter a numerical interger")
a = input("Enter your first number: ")
while (int(a)) >=1000000000000:
print("value too high, enter a lower value")
time.sleep(1)
a = input("Enter your first number: ")
if a.isdigit() == True:
a = int(a)
b = input("Enter your second number: ")
while b.isdigit() == False:
print("enter a numerical interger")
b = input("Enter your second number: ")
while (int(b)) >=1000:
print("value too high, enter a lower value")
time.sleep(1)
b = input("Enter your second number: ")
if b.isdigit() == True:
b = int(b)
print("\n")
print("ANSWER:",a,"To the power of",b,"=",a**b)
print("\n")
def Tconv():
cls()
print("You have selected unit conversion")
print("\n")
print("Enter 1 for conversion from celcius")
print("Enter 2 for conversion from kelvin")
print("\n")
a = input("Enter your choice: ")
if a == "1":
cls()
Tcelc()
elif a == "2":
cls()
Tkelv()
else:
print("Not a valid entry, try again")
time.sleep(1)
cls()
Tconv()
def Tcelc():
print("You have selected conversion from celcius")
print("\n")
a = input("Enter your celcius value: ")
if a.isdigit() == False:
print("Not a valid entry")
time.sleep(1)
cls()
Tcelc()
elif a.isdigit() == True:
print("\n")
print("AWNSER = ",(int(a))+273,"Kelvin")
print("\n")
def Tkelv():
print("You have selected conversion from kelvin")
print("\n")
a = input("Enter your kelvin value: ")
if a.isdigit() == False:
print("Not a valid entry")
time.sleep(1)
Tkelv()
elif a.isdigit() == True:
print("ANSWER = ",(int(a))-273,"Celcius")
print("\n")
def OpEx():
cls()
print("what operation would you like to preform?")
print("\n")
print("Enter 1 for addition")
print("Enter 2 for subtraction")
print("Enter 3 for multliplication")
print("Enter 4 for division")
print("Enter 5 for indice multiplication")
print("Enter 6 for unit conversion")
print("\n")
print("Or type 'close' to exit the program")
print("\n")
task = input("enter your choice: ")
print("\n")
if task == "1":
add()
menu()
elif task == "2":
sub()
menu()
elif task == "3":
multi()
menu()
elif task == "4":
divide()
menu()
elif task == "5":
indice()
menu()
elif task == "6":
Tconv()
menu()
elif task == "close":
exit()
else:
print ("not a valid entry")
time.sleep(2)
OpEx()
def menu():
Q1 = input("Type 'yes' to preform a calculation type 'no' to exit: ")
if Q1 == "yes":
OpEx()
if Q1 == "no":
print("sorry I could not be of futher service")
time.sleep(1)
exit()
else:
print("\n")
print("Not a valid entry, try again")
print("\n")
time.sleep(1)
cls()
menu()
cls()
menu()
You're converting user input to integers, which don't handle floating point all that well. Try converting to float instead, e.g.:
a = float(a)
I would be cautious taking the input as a float because python and many other languages floats are not represented as they may seem. I would recommend pulling the input as a string and then casting it later.

Categories