0

In my program I have an if statement as my first main but whenever the letter is entered, it requires you to enter it several more times before that works and then if it works, the variable num3 and den3.

class menu:
    def question():
        prnt = print("Which equation would like to do? \n A: Inverse the fracion \n B: Add 2 fractions \n C: Subtract 2 fractions \n D: Multiply 2 fractions \n E: Divide 2 fractions \n F: Square a fraction \n")

    def ask():
        ask = input("")
        return ask

class fractions:
    def numerator():
        ask = menu.ask()
        if ask == "b" or menu.ask() == "B" or ask == "C" or ask == "c" or ask == "D" or ask == "d" or menu.ask() == "e" or menu.ask() == "e":
            num1 = int(input("What is the first numerator? \n"))
            num2 = int(input("What is the second numerator? \n"))
            return num1
            return num2

        elif ask == "a" or ask == "A" or ask == "f" or ask == "F":
            num1 = int(input("What is the numerator? \n"))
            return num1

    def denominator():
        ask = menu.ask()
        if mask == "b" or ask == "B" or ask == "C" or ask == "c" or ask == "D" or menu.ask() == "d" or menu.ask() == "e" or menu.ask() == "e":
            den1 = int(input("What is the first denominator? \n"))
            den2 = int(input("What is the second denominator? \n"))
            return den1
            return den2

        elif ask == "a" or ask == "A" or ask == "f" or ask == "F":
            den1 = int(input("What is the denominator? \n"))
            return den1

class math():
    def math1(a,b):
            num3 = den1
            den3 = num1
            return num3
            return den3

    def math2(a,b):
        if den1 == den2:
            num3 = num1 + num2
            den3 = den1
            return num3
            return den3

        if den1 != den2:
            num1 = num1 * den2
            num2 = num2 * den1
            den3 = den1 * den2
            num3 = num1 + num2
            return num3
            return den3

    def math3(a,b):
        if den1 == den2:
            num3 = num1 - num2
            den3 = den1
            return num3
            return den3

        elif den1 != den2:
            den3 = den1 * den2
            num1 = num1 * den2
            num2 = num2 * den1
            num3 = num1 - num2
            return num3
            return den3

    def math4(a,b):
            num3 = num1 * num2
            den3 = den1 * den2
            return num3
            return den3

    def math5(a,b):
            num3 = num1 * den2
            den3 = den1 * num2
            return num3
            return den3   

    def math6(a,b):
            num3 = num1.fractions().numerator() ** 2
            den3 = den1.fractions().denominator() ** 2
            return num3
            return den3            

    def reduce(a,b):
        maxnum = 1000
        while maxnum > 0:
            if num3 % maxnum == 0 and den3 % maxnum == 0:
                num3 = num3 / maxnum
                den3 = den3 / maxnum
            maxnum = maxnum - 1
        return num3
        return den3

def main():
    start = menu.question()
    ask = menu.ask()

    Numer = fractions.numerator()

    Duner = fractions.denominator()

    if menu.ask() == "A" or menu.ask() == "a":
       math.math1(Numer, Duner)
        num3 = math.math1(num3)
        den3 = math.math1(den3)

    elif menu.ask() == "B" or menu.ask() == "b":
        math.math2(Numer, Duner)
        num3 = math.math2(num3)
        den3 = math.math2(den3)

    elif menu.ask() == "C" or menu.ask() == "c":
        math.math3(Numer, Duner)
        num3 = math.math3(num3)
        den3 = math.math3(den3)

   elif menu.ask() == "D" or menu.ask() == "d":
       math.math4(Numer, Duner)
       num3 = math.math4(num3)
       den3 = math.math4(den3)

    elif menu.ask() == "E" or menu.ask() == "e":
        math.math5(Numer, Duner)
        num3 = math.math5(num3)
        den3 = math.math5(den3)

    elif menu.ask() == "F" or menu.ask() == "f":
        math.math6(Numer, Duner)
        num3 = math.math6(num3)
        den3 = math.math6(den3)

    math.reduce(num3, den3)

    print(num3, '/', den3)

main()

the first sets of if statements just cover letters a-f in both lower and uppercase.

1 Answers1

1

You are calling menu.ask() multiple times. Each time you have it written there in e.g. if menu.ask() == 'a' or menu.ask() == 'A'..., it will call that function (unless/until it short-circuits), which asks for input. Instead, call it once and save it:

ask = menu.ask()
if ask == 'b' or ask == 'B'...
    ...
elif ask == 'a' or...
    ...

Or use the in operator:

ask = menu.ask()
if ask in 'bcdeBCDE':
    ...
elif ask in 'aAfF':
    ...

See Asking the user for input until they give a valid response for more input validation strategies.

Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Would it not be a better Idea to suggest that the OP uses `if ask.lower() in 'bcde'` instead? – Christian Dean Dec 07 '16 at 05:03
  • @leaf - That's in the linked question's top answer. I didn't want to go into all the possible ways to improve input validation here. – TigerhawkT3 Dec 07 '16 at 05:04
  • I made the changes and they work perfectly for the if statement but now after it asks for the numerator and the last one is entered it just goes blank and the denominator doesn't appear, would you happen to know why? – john smith Dec 07 '16 at 05:30
  • @johnsmith - Your custom `math` class is full of errors. You might need to revisit a tutorial. – TigerhawkT3 Dec 07 '16 at 05:35
  • I believe the error is in the fractions class though as it won't get to the denominator part which is before the math class starts. I tried swapping the places of numer and duner but I get the same issue but only with the denominator part appearing instead of the numerator part. – john smith Dec 07 '16 at 05:36
  • @johnsmith - There isn't just one error. Almost every line is wrong. You will need to seek out a tutorial and brush up on variables, scope, classes, functions, and methods. – TigerhawkT3 Dec 07 '16 at 05:38