首页 > 解决方案 > 将用户输入保存为pygame中的变量

问题描述

我正在开发一个游戏,其中用户输入他们想要下注的金额,然后如果他们赢/输,钱包就会更新。在他们输入了他们想要的金额后,Pygame 不会将其识别为变量并且不会继续。我得到的消息是

invalid literal for int() with base 10: '' " 

如何让 Pygame 将输入的金额存储为变量?

money =  50

amount = ""

stake = int(amount)

new_balance = money + stake

标签: pythonpygame

解决方案


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

推荐阅读