我正在写一个谋杀之谜 - 很像Clue。我正在使用词典来存储我的所有信息。我的问题:有没有办法改变从一组整数中提取的字典值?我希望每个游戏在开始新游戏时在字典中改变某些值。现在我专注于角色摆放...我试图找出每场比赛(位置[current_room] [“char”]改变角色位置的最佳方法。一旦我明白了怎么做,我想要将其应用于游戏的其他一些方面 - 创造一个全新的神秘来解决每个游戏的想法。欢迎任何建议!
修改 谢谢你的回答!我不是试图随机化我的整个字典,只是改变每个游戏的某些键的值。我想我可能需要以另一种方式解决这个问题。当我开始工作时,我会更新这篇文章。
最终编辑我正在通过编辑字典来改变我想要“改组”的特定键的值。 locations [1] [“char”] = random.choice([0,1,2])然后根据random.choice中结果的一系列IF语句更改其他值。再次感谢您的帮助。
locations = {
1: {"name": "bedroom",
"msg": "There is a painting on the wall. \nThe window looks out into the garden.",
"char": 2,
"item": "matches",
"west": 2},
2: {"name": "living room",
"msg" : "The room is freezing cold. The fireplace is empty.",
"char": 1,
"Item": "firewood",
"east": 1},
}
characters = {
1: {"name": "doctor crichton",
"msg": "A tall, handsome archeologist home from a dig in Africa."},
2: {"name": "the widow",
"msg": "An beautiful woman with a deep air of sadness."},
}
current_room = 1
current_char = locations[current_room]["char"]
def status():
"""updates player on info on current room"""
print("You are in the " + locations[current_room]["name"])
char_status()
def char_status():
"""compiles character infomation in current room"""
if current_char > 0:
char_room_info()
else:
print("\nThis room is empty.")
def char_room_info():
"""NPC behavior in each room"""
print(characters[current_char]["name"].title() + " is in the room with you.")
status()
答案 0 :(得分:0)
Python字典是无序的,所以我不明白为什么你想要“洗牌”它们。如果你想在dict中选择随机字符串,也许你可以使用一个列表来包装这个字典。
像这样:
.myProgress
我认为你明白了。所以现在访问它们:
Locations = [ {"name": "bedroom...}, {"name": "livingroom"...},...]
你也可以用这个:
Locations[random.rand]["name"]
哪个更容易。
答案 1 :(得分:0)
如果Python3.x那么:
import random
random.choice(list(someDictionary.keys()))
如果Python2.x那么:
import random
random.choice(someDictionary.keys())