Issue
I am trying to make a calculator as homework and using functions in it is necessary. I wrote multiple functions for dividing, multiplying etc. The problem is getting user input.
I wrote the gui(a, b) function that gets a and b as a float input. But when I try to use it with other functions I get the “a and b not defined” error.
Here is the code:
def top(a, b):
a, b = gui()
return a + b
def cik(a, b):
return a - b
def bol(a, b):
return a / b
def carp(a, b):
return a * b
def gui(a, b):
a = float(input("1. Sayıyı Giriniz:"))
b = float(input("2. Sayıyı Giriniz:"))
return a, b
while True:
print("İşlemler:")
print("1: Toplama")
print("2: Çıkarma")
print("3: Çarpma")
print("4: Bölme")
print("5: ÇIKIŞ")
islem = input("İşlem Seçiniz:")
if islem in (1, 2, 3, 4):
if islem == 1:
gui(a, b)
print(a, "+", b, "=", top(a,b))
elif islem == "2":
gui(a, b)
print(a, "-", b, "=", cik(a,b))
elif islem == "3":
gui(a, b)
print(a, "*", b, "=", carp(a,b))
elif islem == "4":
gui(a, b)
print(a, "/", b, "=", bol(a,b))
elif islem == 5:
print("ÇIKIŞ YAPILIYOR...")
break
else:
print("Geçersiz İşlem Numarası!!!")
Solution
Define gui
without the parameters, don't pass a
and b
to gui
because you did not define them. Instead assign gui
's returned values to a
and b
, respectively. Just like you did at top
.
def top(a, b):
a, b = gui()
return a + b
def cik(a, b):
return a - b
def bol(a, b):
return a / b
def carp(a, b):
return a * b
def gui():
a = float(input("1. Sayıyı Giriniz:"))
b = float(input("2. Sayıyı Giriniz:"))
return a, b
while True:
print("İşlemler:")
print("1: Toplama")
print("2: Çıkarma")
print("3: Çarpma")
print("4: Bölme")
print("5: ÇIKIŞ")
islem = input("İşlem Seçiniz:")
if islem in (1, 2, 3, 4):
if islem == 1:
a, b = gui()
print(a, "+", b, "=", top(a,b))
elif islem == "2":
a, b = gui()
print(a, "-", b, "=", cik(a,b))
elif islem == "3":
a, b = gui()
print(a, "*", b, "=", carp(a,b))
elif islem == "4":
a, b = gui()
print(a, "/", b, "=", bol(a,b))
elif islem == 5:
print("ÇIKIŞ YAPILIYOR...")
break
else:
print("Geçersiz İşlem Numarası!!!")
Answered By - Lajos Arpad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.