'how to fix error in python code (elif statement )
This is my code below, it is saying that there is an error but i cannot understand the error (the '^' is pointing at the ':' of the elif statement): File "", line 47 elif: ^ SyntaxError: invalid syntax
'' print ("there are 27 sticks")
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:
''
Solution 1:[1]
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:
''
Solution 2:[2]
Your if tree in the second while loop has the structure if... elif... else but the elif doesn't have a condtion. It's throwing an error because there is no condtion.
Solution 3:[3]
You've declared an elif without a variable to check against, you need to put something like
elif something == 'something else':
do this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | louissasha |
| Solution 2 | Matthew Pattok |
| Solution 3 | James |
