我的Python脚本有什么问题?

2024-10-03 02:31:57 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,这是我的剧本:

#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),这就是我需要帮助的地方。你知道吗

谢谢你!你知道吗


Tags: orthetoanswernewexampleexitquit
2条回答

给你。我已经编辑了你的代码。让我知道如果这是你想要的,我不知道你为什么用print "",我把它移到这里,因为它伤害了我的眼睛!:P.也请不要使用exit作为用户的输入使用“quit”最好。解释一下可以在评论中找到。你知道吗

#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

这是更整洁和Python。你知道吗

此位看起来可疑,它(可能)需要一个字符串并尝试将其转换为浮点:

    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)

相关问题 更多 >