繁体   English   中英

如何修复 python 代码中的错误(elif 语句)

[英]how to fix error in python code (elif statement )

这是我下面的代码,它说有错误但我无法理解错误('^' 指向 elif 语句的':'):文件“”,第 47 行 elif:^ SyntaxError:无效句法

'' print ("有27根木棍")

sticks = 27 

player1 = True 
Ai = False 

print ("there are 27 sticks, pick the last one !")

print("1")
print("2")
print("3")

while player1 == True:
    
    inp = int(input("Enter the number of sticks you want to take: "))

    if inp == 1:
        inp = "1: 1 stick taken"
        sticks = sticks -1
        print (sticks)
        player1 = False 
        Ai = True 
    elif inp == 2:
        inp = "2 sticks taken"
        sticks = sticks - 2
        print (sticks)
        player1 = False 
        Ai = True 
    elif inp == 3:
        inp = "3 sticks taken"
        sticks = sticks - 3
        print (sticks)
        player1 = False 
        Ai = True 
    else:
        print("Invalid input!")
        
        
while Ai == True:
    if sticks == 7:
        InpAi = 3
        sticks = sticks - InpAi
        print (sticks)
        player1 = True 
        Ai = False 
    elif: 
        print ("")
    
    else: 
        ''
    
    

第二个while循环中的if树具有结构if... elif... elseelif没有条件。 它抛出错误,因为没有条件。

您已经声明了一个没有要检查的变量的 elif,您需要输入类似

elif something == 'something else':
    do this
while Ai == True:
    if sticks == 7:
        InpAi = 3
        sticks = sticks - InpAi
        print (sticks)
        player1 = True 
        Ai = False 
    elif: # <= add condition here if any otherwise remove that line :)
        print ("")
    
    else: 
        ''

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM