Python Forum
Calculator problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Calculator problem (/thread-11213.html)



Calculator problem - BeginnerCoderPaero - Jun-28-2018

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")



RE: Calculator problem - j.crater - Jun-28-2018

Hello, please put your code in Python code tags. You can find help here.


RE: Calculator problem - Larz60+ - Jun-28-2018

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.


RE: Calculator problem - buran - Jun-28-2018

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


RE: Calculator problem - BeginnerCoderPaero - Jun-29-2018

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