Catherine
Catherine

Reputation: 23

Python Invalid Syntax Errors

I am trying to make a python code for the game of roulette, but every line is returning "invalid syntax". It might be some sort of indentation error, but I am new at Python and can't figure it out for the life of me. Any help would be much appreciated!

import random

def roulette():
        "Game of roulette"
        chips=10
        L=[1,2,3,4,5,6,7,8,9,10]
        while chips > 0:
                x=int(input("You have 10 chips. How many do you want to bet? ")
                while x not in L:
                        x=int(input("You bet between 1 and 10 chips. Bet 10 or less chips. ")
                y=input("What do you want to bet on? Green, Red or Black? ")
                z=random.randint(0,10)
                print(z)
                if x == z:
                        chips=chips+(9*x)
                        print("You have %i chips" %chips)
                elif ((y.lower() == "green" and z == 0) or (y.lower() == "red" and z in [1,3,5,7,9]) or (y.lower() == "black" and z in [2,4,6,8,10])):
                        chips=chips+x
                        print("You have %i chips" %chips)
                else:
                        chips=chips-x
                        print("You lost. You now have %i chips" %chips)
                w=input("Do you want to play another round? Yes or No? ")
                if w.lower() == "no" or chips == 0:
                        print("The game is over! You had %i chips left" %chips)
                        break

roulette()

Upvotes: 0

Views: 142

Answers (2)

gelonida
gelonida

Reputation: 5630

You missed two closing parenthesises.

In general it is important to post the exact syntax error. Very often it is the line above the syntax error, that as a parenthesis missing or a double quote missing.

As @ivysaur-yw pointed out, things like this are simpler to discover if you use an editor or an IDE with python syntax highlighting

Here the corrected version:

#!/usr/bin/env python
import random

def roulette():
        "Game of roulette"
        chips=10
        L=[1,2,3,4,5,6,7,8,9,10]
        while chips > 0:
                x=int(input("You have 10 chips. How many do you want to bet? "))  # one ) was missing here
                while x not in L:
                        x=int(input("You bet between 1 and 10 chips. Bet 10 or less chips. ")) # one ) was missing here
                y=input("What do you want to bet on? Green, Red or Black? ")
                z=random.randint(0,10)
                print(z)
                if x == z:
                        chips=chips+(9*x)
                        print("You have %i chips" %chips)
                elif ((y.lower() == "green" and z == 0) or (y.lower() == "red" and z in [1,3,5,7,9]) or (y.lower() == "black" and z in [2,4,6,8,10])):
                        chips=chips+x
                        print("You have %i chips" %chips)
                else:
                        chips=chips-x
                        print("You lost. You now have %i chips" %chips)
                w=input("Do you want to play another round? Yes or No? ")
                if w.lower() == "no" or chips == 0:
                        print("The game is over! You had %i chips left" %chips)
                        break

roulette()

With corrected I mean syntactically correct. I did not check the functionality

Upvotes: 0

ivysaur-yw
ivysaur-yw

Reputation: 156

You missed a closing braces on line 10.

while x not in L:
    x=int(input("You bet between 1 and 10 chips. Bet 10 or less chips. "))

When Python gives you syntax error such as Unexpected Token or Expected ')' but found '}', always look one line above it. There's a great chance you forgot to close some parenthesis.

On a side note, you should consider using proper IDE with syntax checking. Visual Studio Code is a great example.

Upvotes: 1

Related Questions