Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator problem
#1
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")
Reply
#2
Hello, please put your code in Python code tags. You can find help here.
Reply
#3
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.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner. Calculator problem ¯\_(ツ)_/¯ stykus1992 0 2,291 Feb-15-2018, 11:01 AM
Last Post: stykus1992
  Tip calculator problem Dhaval 1 4,426 Jun-06-2017, 12:49 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020