我需要写一个程序来确定输入的公式是否有一组等价的括号

2024-06-16 14:44:56 发布

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

这是我的代码,它确实运行,但输出不是它应该的方式

formula = input("Enter a formula: ")
parenthesis = (formula.count("(,)"))
if parenthesis >= 1 :
    print ("You have enteread a complete formula.")
else:
    print ("You have incomplete set of parenthesis")

Tags: 代码youinputifhavecount方式else
1条回答
网友
1楼 · 发布于 2024-06-16 14:44:56

也许我的想法过于简单化了,但这里有一个简单的方法:

    formula = input("Enter a formula: ")
    open_brackets = formula.count("(")
    close_brackets = formula.count(")")
    if open_brackets == close_brackets:
        print("You have entered a complete formula.")
    else:
        print("You have incomplete set of parenthesis")

相关问题 更多 >