Python计算器 - 我怎样才能做得更好

时间:2014-12-29 07:43:27

标签: python

我对编程很新。我在python中创建了这个计算器,我想知道如何才能使它更高效,因为它对我来说似乎有点低效。我仍然不熟悉python和一些编程概念,所以我很高兴能够了解不同的方法。也许我有更好的方法来设置功能,或者可以将它们放入不同的类中? 感谢您的回复

def add():
    a = int(raw_input("Enter a number!: \n"))
    b = int(raw_input("Enter a number!: \n"))
    c = a + b
    if a + b == c:
        print c 
        menu()
    return c 
def sub():
    a = int(raw_input("Enter a number!: \n"))
    b = int(raw_input("Enter a number!: \n"))
    c = a - b
    if a - b == c:
        print c 
        menu()
    return c 

def mul():
    a = int(raw_input("Enter a number!: \n"))
    b = int(raw_input("Enter a number!: \n"))
    c = a * b
    if a * b == c:
        print c 
        menu()
    return c 

def div():
    a = float(raw_input("Enter a number!: "))
    b = float(raw_input("Enter a number!: "))
    c = a / b
    if a / b == c:
        print c 
        menu()
    return c 

def square():
    a = int(raw_input("Enter a number!: \n"))
    c = a * a
    if a * a == c:
        print c 
        menu()
    return c 

def menu():
    print """
    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    5. Square
    6. Exit
    """

    choices = [1, 2, 3, 4, 5, 6]
    choice = int(raw_input("Enter a number!: \n"))

    while True:
        if choice == 1:
            print add()
        elif choice == 2:
            print sub()
        elif choice == 3:
            print mul()
        elif choice == 4:
            print div()
        elif choice == 5:
            print square()
        elif choice == 6:
            exit()
        else:
            for choose in choices:
                if choice != choices:
                    print "Please enter a number 1 - 6!"
                    menu()


menu()

3 个答案:

答案 0 :(得分:0)

在所有功能中有几件事要重复。就像要求用户输入两个数字a& b使用raw_input倍数次。可以将它们放入另一个方法getTwoNumbers(),它将从用户获取两个输入并返回两个数字。需要在正在执行二进制操作的函数中调用此方法(即getTwoNumbers())。您还将结果存储到另一个变量c中,该变量可以被省略,因此也不需要与变量c的二进制运算进行比较。如果您可以根据要执行的当前二进制操作执行return a+breturn a-b等,则会更好。

答案 1 :(得分:0)

首先,在else用户输入无效菜单编号的情况下,您不需要这些内容:

        for choose in choices:
            if choice != choices:

毕竟,您已经知道choice无效。

其次,当用户输入6而不是exit()时,您应该return。该计划仍将结束。它经常被认为是粗鲁的"对于直接调用exit()的函数 - 让调用者决定函数结束时该做什么。

第三,当您的运营商成功时,您不需要致电menu()。你做的很奇怪,因为menu()无论如何都包含它自己的无限循环。这只是多余的。

最后,通过将实际操作作为参数传递,可以将所有双参数数学函数合并为一个。 operator模块使这更容易。

将所有这些想法放在一起,这里是新代码:

import operator

def dobinaryop(op):
    a = float(raw_input("Enter a number!: \n"))
    b = float(raw_input("Enter a number!: \n"))
    return op(a, b)

def square():
    a = float(raw_input("Enter a number!: \n"))
    return a * a

def menu():
    while True:
        print """
    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    5. Square
    6. Exit
    """

        choice = int(raw_input("Enter a number!: \n"))

        if choice == 1:
            print dobinaryop(operator.add)
        elif choice == 2:
            print dobinaryop(operator.sub)
        elif choice == 3:
            print dobinaryop(operator.mul)
        elif choice == 4:
            print dobinaryop(operator.div)
        elif choice == 5:
            print square()
        elif choice == 6:
            exit()
        else:
            print "Please enter a number 1 - 6!"

menu()

你可以通过将各种运算符放入一个列表并按choice编制索引来进一步简化这一点,但是由于你只有四个这样的运算符,加上两个特殊的运算符,因为没有太多的好处而有点晦涩难懂案件和范围检查。

答案 2 :(得分:-1)

将函数和相关索引放在字典中。使用该字典来决定执行哪个函数。

并获得每个功能的重复部分。我认为这些解决方案可以让您的代码更好。