私は、数式に一致するかっこが含まれているかどうかを判断するプログラムを作成しようとしています。私は彼らが左と右の同じ量を持っているかどうかを確認する必要があり、次にそれが開いているかどうかを判断する必要があります。しかし、私はそれをどうやって行うのか分かりません。私は表現を取得した後、何も出ていない。彼らが閉じているかどうかを調べる良い方法があると知っていますが、わかりません。閉じたか開いている括弧のチェッカー
(5 + 5)/(5 + 2) 5)、またはその
def main():
left = 0
right = 0
even = (0 or 2 or 4 or 6 or 8 or 10 or 12 or 14) #is there a better way to check if they match rather than doing even or odd?
odd = (1 or 3 or 5 or 7 or 9 or 11 or 13 or 15)
if expression == "(":
left += 1
elif expression == ")":
right -= 1
expression = input("write a mathematical expression with parentheses") #lets user input mathematical expression to evaluate for correct number of parentheses
parentheses = (left + right) #this is probably not the most efficient way, I just want to find out if my parentheses match, so suggestions here would help
if parentheses == even:
print("Your parentheses are closed")
if parentheses == odd:
print("You are missing a parenthese")
main()
あなたの式は何のように見えることができますか?私たちに例を挙げてください。 – roelofs
あなたは式の文字列を反復処理していないことを知っていますか? – roelofs
(1-9)* 4 – savannah