python - Python - 如何根据用户输入更改字典中的值?(“关上盒子”游戏)
问题描述
使用一些 Python 基础知识在我的第一个程序上工作的极端新手。我正在尝试创建“关闭盒子”桌面游戏,玩家尝试通过掷一对骰子来消除一行数字(在本例中为 1-10),然后根据掷骰子的总和删除数字.
在我的程序中,我有一个 Board 字典(theBoard),我想根据用户输入要删除的数字来更改字典中的各种值。例如,用户掷出 5,他们输入 5(但也可以输入 2 和 3)从棋盘上删除,所以棋盘现在是 1-2-3-4-X-5-6-7-8- 9-10。
下面的当前代码,你会在最后看到我试图检查输入是否在字典中。我想检查输入是否在字典中,如果是,我想将该值更改为 X:
#setting up the Board to show which number slot we're in, which will have an integer
#until it is selected by the user to be removed from the board, then it will be 'X'
theBoard = {'one': '1', 'two':'2', 'three': '3', 'four': '4', \
'five':'5', 'six': '6', 'seven': '7', 'eight':'8', 'nine': '9', \
'ten': '10'}
#printing the visual of the board, starts with showing each integer 1 thru 10
def printBoard(board):
print(board['one'] + '|' + board['two'] + '|' + board['three'] + '|' \
+ board['four'] + '|' + board['five'] + '|' + board['six'] + '|' \
+ board['seven'] + '|' + board['eight'] + '|' + board['nine'] + '|' \
+ board['ten'] + '|')
#first attempt at coding a dice roll
diceRoll = random.randint(1, 6) + random.randint(1,6)
print('Welcome! Here is your board, let\'s get started.')
printBoard(theBoard)
print('To begin, roll the pair of dice by typing \'r\'.')
rollOne = input()
#the user input triggers a dice roll
if rollOne == 'r':
print('You rolled a ' + str(diceRoll) + '. Now choose which \
numbers to remove from the board.')
print('Here\'s the board again, you can type a single number, or multiple \
numbers separated by a commma to remove them from the board.')
printBoard(theBoard)
newBoard = input()
if str(newBoard) in theBoard:
print('yes')
else:
print('no')```
解决方案
推荐阅读
- haskell - 遍历列表并填充结果列表 [Haskell]
- bash - [[ $(echo ${lines} | grep \'_SUCCESS\') ]] 是什么意思?
- android - 将 JNA 绑定到 Xamarin.Android 绑定库时出错
- coinbase-api - 调用 Coinbase NODEJS API 时出现 UNABLE_TO_GET_ISSUER_CERT_LOCALLY 错误
- php - 上传图片 ajax 复制上传的图片,除非页面重新加载
- c++ - 通过 WebRTC 发送已编码的帧
- r - 如何使用 for 循环对具有不同程度程度的不同多项式回归模型的结果执行 ANOVA
- php - 如何在 PHP 代码中不进行无限循环重定向?
- java - Java - 将原始电子邮件内容文本 RFC 822 转换为 MimeMessage
- c# - 在asp.net核心中,空字符串是否转换为NULL?