我的python脚本有什么问题?

时间:2014-11-05 04:38:39

标签: python math decimal

所以,这是我的剧本:

#Imports
import decimal
#variables
neweq = "neweq"
on = 1
#loop
while on > 0:
#equasion function
    def eq ():
        global b
        b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  ")
        print ""
        print ""
        print ""
        print ""
        b = float(b)
        b = '%.3f'%(b)
        if (b==exit):
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            exit ("Thank you for using me :)")
#input funcution
    def inp ():
        a = input("Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  ")
        if (a==exit):
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            print ""
            exit ("Thank you for using me :)")
        if (a == neweq):
            print ""
            print ""
            a = 0
            eq ()
            inp()
        if (b==a):
            print ""
            print "Yes, the answer is", a
            print ""
            print ""
            eq ()
        else:
            print ""
            print "No, the answer is not", a
            print ""
            print ""
            print "test line", b
            inp ()
#function calls
    eq()
    inp ()

问题?

Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  2.0/4.0




Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  1/2

No, the answer is not 0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  102.0

No, the answer is not 102.0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  1.0/2.0

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  .500

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  0

No, the answer is not 0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  4.0/2.0

No, the answer is not 2.0


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  2.0/4.0

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  0.500

No, the answer is not 0.5


test line 0.500
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  

它应该返回'是的,答案是0.5'但是,它不是。与其他几个方面相同。我无法弄清楚它有什么问题,但我怀疑它是b = '%.3f'%(b),而我需要帮助。

谢谢!

2 个答案:

答案 0 :(得分:1)

你去吧。我已经编辑了你的代码。让我知道这是否是你想要的,我不知道你为什么使用print ""我在这里删除它因为它伤害了我的眼睛! :P。另外,请不要使用exit作为用户输入使用"退出"可以在评论中找到解释。

#Imports
import decimal
#variables
neweq = "neweq"
on = 1
#loop
#equasion function
def eq ():
        global b
        b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  ")
        if b == exit:
             exit ("Thank you for using me :)")
        else:
            b = float(b)        ## input converted into float.
            b = '%.3f'%(b)      ## after this b would be of type string
            b = float(b)        ## again converting into float to match with "a" in `inp()` 
#input funcution
def inp ():
        a = input("Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  ")
        if a == exit:
            exit ("Thank you for using me :)")
        if a == neweq:
            a = 0
            eq ()
            inp()
        if b == a:          ## if a == b should work now.  
            print "Yes, the answer is", a
            eq ()
        else:
            print "No, the answer is not", a
            print "test line", b
            inp ()
#function calls
eq()
inp ()

如果你使用print""来避免工作空间的混乱,那么试试这个

   print "\n"*5  ## You have 5 empty lines. Replace the number 5 as per your needs

这更加整洁和pythonic。

答案 1 :(得分:0)

这一点看起来很可疑,它(大概)需要一个字符串并尝试将其转换为浮点数:

    b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  ")
    b = float(b)