python - 将用户输入保存为pygame中的变量
问题描述
我正在开发一个游戏,其中用户输入他们想要下注的金额,然后如果他们赢/输,钱包就会更新。在他们输入了他们想要的金额后,Pygame 不会将其识别为变量并且不会继续。我得到的消息是
invalid literal for int() with base 10: '' "
如何让 Pygame 将输入的金额存储为变量?
money = 50
amount = ""
stake = int(amount)
new_balance = money + stake
解决方案
amount = raw_input('Please enter a number: ')
如果你想获得用户输入。
此外,您可以使用错误处理:
amount = raw_input('...')
stake = 0
try:
stake = int(amount)
except Exception: # this is raised if the input is invalid i.e. not numeric
pass # does nothing
推荐阅读
- android - Open internal storage image with Intent.ACTION_EDIT exception with FileProvider android
- c# - How to implement dataTables Server side Side Paging/Searching/Sorting in ASP.net Core
- security - Microsoft Graph - How to test the /Security/Alerts webhook
- spring-boot - Spring Boot - 如何将 src/main/resources 子目录包含到类路径中
- javascript - 函数内部未定义的全局变量
- database - 完整和增量数据加载 - api
- asammdf - Asammdf 提取更多元数据
- identityserver4 - 没有客户端库的 IdentityServer4 无提示手动更新
- reactjs - Html React 解析器不渲染为空
标签
- flutter - 如何使用 Flutter 将 2 个浮动操作按钮固定在某些特定位置?