python OOP if语句需要垃圾邮件和未定义的变量

时间:2016-12-07 04:54:50

标签: python oop variables python-3.5

在我的程序中,我有一个if语句作为我的第一个主要内容但是无论何时输入该字母,它都要求你在它工作之前多次输入它,然后如果它有效,则变量num3和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()

第一组if语句只包括小写字母a-f。

1 个答案:

答案 0 :(得分:1)

您多次致电menu.ask()。每次你把它写在例如if menu.ask() == 'a' or menu.ask() == 'A'...,它将调用该函数(除非/直到它发生短路),它要求输入。相反,请将其调用一次并保存:

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

或使用in运算符:

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

有关更多输入验证策略,请参阅Asking the user for input until they give a valid response