Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using input in a function and using variables

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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ı!!!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading