首页 > 解决方案 > Python 无效的语法错误

问题描述

我正在尝试为轮盘游戏制作 python 代码,但每一行都返回“无效语法”。这可能是某种缩进错误,但我是 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? ")
                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()

标签: python

解决方案


你错过了第 10 行的右大括号。

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

当 Python 给出诸如Unexpected Tokenor之类的语法错误时Expected ')' but found '}',请始终查看其上方的一行。您很有可能忘记关闭一些括号。

附带说明一下,您应该考虑使用带有语法检查的适当 IDE。Visual Studio Code 就是一个很好的例子。


推荐阅读