用Python重启我的加速计算器?

时间:2013-11-28 14:14:21

标签: python function class calculator

我制作了一个简单的python加速计算器,但我遇到的问题是让程序返回到开始或退出。我希望用户能够点击' 1'并重新启动计算器或点击“' 2'并让程序退出。我似乎无法弄清楚如何在程序完成等式后运行我的主函数,任何建议?感谢。

class equation():

    def main():
        Calc()
        while True:
            restart = input("Would you like to run again? If yes press '1' if you wish to Exit press '2'")
            if restart==1:
                Calc()
            else:
                print "Goodbye"
                break

    print "What is your Velocity: "
    v = float(input()) #m/s
    print "What is your Intial Velocity: "
    u = float(input()) #m/s
    print "What is the time: "
    t = float(input()) #Seconds
    aa = v-u
    answer = aa/t

eq=equation()

print "Your Acceleration is: ", eq.answer, "m/s^2"

print eq.main

2 个答案:

答案 0 :(得分:0)

您的缩进是错误的,整个if必须缩进4个空格,并在break中添加else

输入也接收字符串,而不是数字。

答案 1 :(得分:0)

希望我理解你的问题,我也建议你总是写一个基本的程序,然后继续复杂的逻辑,并继续建立在你设置的平台上。

所以我编写了一个基本程序,可以让用户输入决定计算或退出。如果是计算,那么它将从用户那里获得两个数字,然后进行添加并返回以等待是退出还是继续进行计算。

!在/ usr / bin中/ Python的

class equation:


        def calculate(self):
                print 'Enter the first number'
                input1 = int(raw_input())
                print 'Enter the second number'
                input2 = int(raw_input())
                result = input1 + input2
                print result

        def mainfun(self):
                while True:
                        print 'Enter 1 for calculate operation and any to exit'
                        userInput = str(raw_input())
                        if userInput=='1':
                                self.calculate()
                        else:
                                print 'Good bye'
                                break

operation = equation()

operation.mainfun()

我已经测试了代码,但它确实有用。