当右括号“)”没有开始的开括号“(”时,编写程序以打印“无效输入”)

2024-04-25 15:23:35 发布

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

str=")(hi)(hello))"

lcount=0
rcount=0
for i in str:
    print(i)
    if i == "(":
        lcount+=1
    if i == ")":
        rcount+=1
print(lcount, rcount)

if lcount == rcount:
    print("there's an even number of ( and )s")

if i[0] == ")":
    print("invalid input")

现在我可以让它在第一个字符是右括号时打印“无效输入”,但我无法让它在所有没有相应的(


3条回答

以下是我的建议(以及我编写代码时@JohnGordon告诉您的内容):

str=")(hi)(hello))"                                                                                                                                                                                                                          
                                                                                                                                                                                                                                               
count = 0                                                                                                                                                                                                                                    
for i in str:                                                                                                                                                                                                                                
    if i == "(":                                                                                                                                                                                                                             
        count += 1                                                                                                                                                                                                                           
    elif i == ")":                                                                                                                                                                                                                           
        count -= 1
    if count < 0:
        break                                                                                                                                                                                                                           
                                                                                                                                                                                                                                               
if count == 0:                                                                                                                                                                                                                               
    print("there's an even number of ( and )s")                                                                                                                                                                                              
else:                                                                                                                                                                                                                                        
    print("invalid input")
                                                                                                                                                                                                             
if i[0] == ")":
    print("invalid input")

上面的代码片段检查变量i[0],在本例中,该变量存储最新的&;上面for循环中的最后一个字符。在您的情况下,它将存储),并且将打印“invalid input”语句,只要您的输入字符串以)结尾,就会打印此语句。我相信这是你不想要的

if lcount == rcount and mystr[0] != ")":
    print("there's an even number of ( and )s")
else:
    print("invalid input")

假设您正在检查第一个字符,如果它以一个开括号开头,请在变量中进行更改,如上所示。另外,我强烈建议在命名变量时避免变量str

以下是另一种可能的解决方案:

string = ")(hi)(hello))"

# Count left and right parentheses, then compare the result
# Raise a ValueError (with a custom message) if counts aren't equal.
if string.count("(") != string.count(")"):
    raise ValueError("Unbalanced parentheses detected!")

另外,不要使用strlistbool等变量名,否则会造成混乱和混乱

相关问题 更多 >