python - 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()
解决方案
你错过了第 10 行的右大括号。
while x not in L:
x=int(input("You bet between 1 and 10 chips. Bet 10 or less chips. "))
当 Python 给出诸如Unexpected Token
or之类的语法错误时Expected ')' but found '}'
,请始终查看其上方的一行。您很有可能忘记关闭一些括号。
附带说明一下,您应该考虑使用带有语法检查的适当 IDE。Visual Studio Code 就是一个很好的例子。
推荐阅读
- python - Excel 数据以错误的格式读取
- reactjs - 错误:useRecoilState 的参数无效:需要原子或选择器,但未定义
- php - AWS EC2 ubuntu 服务器无法使用 SMTP 发送电子邮件
- jdbc - Kafka jdbc 连接器作为变更数据捕获
- android - 根视图层次结构的父级
- python - 如何将指数趋势线添加到 ArcPy 中的半对数散点图中?
- wix - 使用管理员权限运行 wix 安装程序时访问被拒绝 SELECT `Message` FROM `Error` WHERE `Error` = 1708
- java - 获取构建错误必须在 gradle 中指定绝对路径
- leaflet - 传单双图
- javascript - 如何在使用按钮时为用户输入使用参数而不是将其与 document.getElementById 连接?