Python Forum

Full Version: Calculator problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?

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")
Hello, please put your code in Python code tags. You can find help here.
I added code tags for you this time, please do so in future posts,

Also, show complete unedited error report.
I contains valuable information leading up to the error.
Please, always post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line.
Take a time to read What to include in a post

In this case check your spelling, i.e. osamaara and osammara
Valitse operaatio (+, - , /, *): +
Anna luku 1: aasi
Ei tämä ole mikään luku
Anna luku 2: aasi
ei tämä ole mikään luku
Traceback (most recent call last):
File "C:\Users\Omistaja\Desktop\laskin.py", line 29, in <module>
luku_1, luku_2 = valitse_muuttujat()
File "C:\Users\Omistaja\Desktop\laskin.py", line 13, in valitse_muuttujat
return luku_1, luku_2
UnboundLocalError: local variable 'luku_1' referenced before assignment

Plus the problem with the "Tulos: none" is that there isnt any error message so the program works but it should only show "Tällä ohjelmalla ei pääse äärettömyyteen". I dont know if you guys understood but thanks for the help anyways