0

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()
4
  • 3
    'every line is returning "invalid syntax"'—every line? What is the exact error message? Please copy and paste it into your question. Commented Apr 6, 2020 at 1:27
  • You're missing closing parentheses on the two lines where you set x. I figured this out by running your code through Pylama, a linter. BTW welcome to SO! Please take the tour and read How to Ask. In the future for debugging questions, you need to make a minimal reproducible example including the full error message. Commented Apr 6, 2020 at 1:29
  • you might consider accepting one of the given answers. If you really think none of the answers helped you, then add a comment and say so Commented Apr 20, 2020 at 12:06
  • Did any of the answers help? Just asking as you didn't accept any answer. Commented May 6, 2020 at 10:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.