So im trying to code a calculator and it doesnt work i cant figure out whats the problem with the code. The problem especially come with dividing since when i divide by zero it should only print "Tällä ohjelmalla ei pääse äärettömyyteen" but when i use it it replies accordingly but also adds underneath it "Tulos: none" which shouldnt happen. Incase you wonder it means in english "Result: None" it complains that
unboundlocalerror local variable luku_2 referenced before assignment. Any help?
unboundlocalerror local variable luku_2 referenced before assignment. Any help?
operaattori = input("Valitse operaatio (+, - , /, *): ")
def valitse_muuttujat():
try:
luku_1 = float(input("Anna luku 1: "))
except ValueError:
print("Ei tämä ole mikään luku")
try:
luku_2 = float(input("Anna luku 2: "))
except ValueError:
print("ei tämä ole mikään luku")
return luku_1, luku_2
def summa(luku_1, luku_2):
return luku_1 + luku_2
def jakolasku(luku_1, luku_2):
try:
osamaara = (luku_1 / luku_2)
except ZeroDivisionError:
print("Tällä ohjelmalla ei pääse äärettömyyteen")
else: return osammara
def kertolasku(luku_1, luku_2):
return luku_1 * luku_2
def erotus(luku_1, luku_2):
return luku_1 - luku_2
if operaattori == "+":
luku_1, luku_2 = valitse_muuttujat()
print("Tulos: {}".format(summa(luku_1, luku_2)))
elif operaattori == "-":
luku_1, luku_2 = valitse_muuttujat()
print("Tulos: {}".format(erotus(luku_1, luku_2)))
elif operaattori == "/":
luku_1, luku_2 = valitse_muuttujat()
print("Tulos: {}".format(jakolasku(luku_1, luku_2)))
elif operaattori == "*":
luku_1, luku_2 = valitse_muuttujat()
print("Tulos: {}".format(kertolasku(luku_1, luku_2)))
else:
print("Operaatiota ei ole olemassa")
