尝试根据字典中的值设置变量
Trying to set a variable from a value in a dictionary
我正在学习 python 并且正在制作剪刀石头布游戏。
我卡在了一部分上。
我目前有 4 个变量(虽然我想减少到 2 个)
- pKey
- p选择
- comKey
- comChoice
他们分别在字典中查找 Key 和 Value。
choice = {1:'rock', 2:'paper', 3:'scissors'}
我遇到的问题是使用变量从字典中获取键。
这是给我带来麻烦的代码片段
print('--- 1 = Rock 2 = Paper 3 = Scissors --- ')
pKey = input() # this is sets the key to the dictionary called choice
while turn == 1: # this is the loop to make sure the player makes a valid choice
if pKey == 1 or 2 or 3:
pChoice = choice.values(pKey) # this calls the value from choice dict and pKey variable
break
else:
print('Please only user the numbers 1, 2 or 3 to choose')
comKey = random.randint(1, 3) # this sets the computer choices
comChoice = choice.values(comKey)
具体比较麻烦的地方是
pChoice = choice.values(pKey)
和
comChoice = choice.values(comKey)
我尝试了所有我知道的方法,包括使用括号、尝试不同的方法和使用不同的格式。
很想学这个!谢谢!
你不知道如何从字典中获取元素,你的代码应该是这样的:
import random
choice = {1: 'rock', 2: 'paper', 3: 'scissors'}
print('1 = Rock\t2 = Paper\t3 = Scissors')
pKey = int(input())
if pKey in (1, 2, 3):
pChoice = choice[pKey]
else:
print('Please only user the numbers 1, 2 or 3 to choose')
pChoice = 'No choice'
comKey = random.randint(1, 3)
comChoice = choice[comKey]
print(pChoice, comChoice)
适合我。
听起来你只是想查字典
pKey = 1
pChoice = choices[pKey] # rock
dict.values
用于创建包含字典所有值的列表(实际上是一个 dict_values
对象)。它不用作查找。
就您的代码结构而言,它可能需要一些工作。 rock/paper/scissors 选择对于 Enum
来说是完美的,但现在你可能有点难以接受。让我们尝试作为顶层模块常量。
ROCK = "rock"
PAPER = "paper"
SCISSORS = "scissors"
def get_choice():
"""get_choice asks the user to choose rock, paper, or scissors and
returns their selection (or None if the input is wrong).
"""
selection = input("1. Rock\n2. Paper\n3. Scissors\n>> ")
return {"1": ROCK, "2": PAPER, "3": SCISSORS}.get(selection)
将它们作为常量寻址可确保它们在您的代码中处处相同,否则您会得到一个非常明确的 NameError(而不是 if 分支未执行,因为您 if comChoice == "scisors"
)
带有枚举的最小示例如下所示:
from enum import Enum
Choices = Enum("Choices", "rock paper scissors")
def get_choice():
selection = input(...) # as above
try:
return Choices(int(selection))
except ValueError:
# user entered the wrong value
return None
您可以通过使用更详细的枚举定义来扩展它,并教每个 Choice 实例如何计算获胜者:
class Choices(Enum):
rock = ("paper", "scissors")
paper = ("scissors", "rock")
scissors = ("rock", "paper")
def __init__(self, loses, beats):
self._loses = loses
self._beats = beats
@property
def loses(self):
return self.__class__[self._loses]
@property
def beats(self):
return self.__class__[self._beats]
def wins_against(self, other):
return {self: 0, self.beats: 1, self.loses: -1}[other]
s, p, r = Choices["scissors"], Choices["paper"], Choices["rock"]
s.wins_against(p) # 1
s.wins_against(s) # 0
s.wins_against(r) # -1
不幸的是,没有很好的方法来丢失其中的抽象(每次调用时将 "paper" 抽象为 Choices.paper)因为你不知道 Choices["paper"]
是什么时候 Choices.rock
被实例化。
我正在学习 python 并且正在制作剪刀石头布游戏。
我卡在了一部分上。
我目前有 4 个变量(虽然我想减少到 2 个)
- pKey
- p选择
- comKey
- comChoice
他们分别在字典中查找 Key 和 Value。
choice = {1:'rock', 2:'paper', 3:'scissors'}
我遇到的问题是使用变量从字典中获取键。
这是给我带来麻烦的代码片段
print('--- 1 = Rock 2 = Paper 3 = Scissors --- ')
pKey = input() # this is sets the key to the dictionary called choice
while turn == 1: # this is the loop to make sure the player makes a valid choice
if pKey == 1 or 2 or 3:
pChoice = choice.values(pKey) # this calls the value from choice dict and pKey variable
break
else:
print('Please only user the numbers 1, 2 or 3 to choose')
comKey = random.randint(1, 3) # this sets the computer choices
comChoice = choice.values(comKey)
具体比较麻烦的地方是
pChoice = choice.values(pKey)
和
comChoice = choice.values(comKey)
我尝试了所有我知道的方法,包括使用括号、尝试不同的方法和使用不同的格式。
很想学这个!谢谢!
你不知道如何从字典中获取元素,你的代码应该是这样的:
import random
choice = {1: 'rock', 2: 'paper', 3: 'scissors'}
print('1 = Rock\t2 = Paper\t3 = Scissors')
pKey = int(input())
if pKey in (1, 2, 3):
pChoice = choice[pKey]
else:
print('Please only user the numbers 1, 2 or 3 to choose')
pChoice = 'No choice'
comKey = random.randint(1, 3)
comChoice = choice[comKey]
print(pChoice, comChoice)
适合我。
听起来你只是想查字典
pKey = 1
pChoice = choices[pKey] # rock
dict.values
用于创建包含字典所有值的列表(实际上是一个 dict_values
对象)。它不用作查找。
就您的代码结构而言,它可能需要一些工作。 rock/paper/scissors 选择对于 Enum
来说是完美的,但现在你可能有点难以接受。让我们尝试作为顶层模块常量。
ROCK = "rock"
PAPER = "paper"
SCISSORS = "scissors"
def get_choice():
"""get_choice asks the user to choose rock, paper, or scissors and
returns their selection (or None if the input is wrong).
"""
selection = input("1. Rock\n2. Paper\n3. Scissors\n>> ")
return {"1": ROCK, "2": PAPER, "3": SCISSORS}.get(selection)
将它们作为常量寻址可确保它们在您的代码中处处相同,否则您会得到一个非常明确的 NameError(而不是 if 分支未执行,因为您 if comChoice == "scisors"
)
带有枚举的最小示例如下所示:
from enum import Enum
Choices = Enum("Choices", "rock paper scissors")
def get_choice():
selection = input(...) # as above
try:
return Choices(int(selection))
except ValueError:
# user entered the wrong value
return None
您可以通过使用更详细的枚举定义来扩展它,并教每个 Choice 实例如何计算获胜者:
class Choices(Enum):
rock = ("paper", "scissors")
paper = ("scissors", "rock")
scissors = ("rock", "paper")
def __init__(self, loses, beats):
self._loses = loses
self._beats = beats
@property
def loses(self):
return self.__class__[self._loses]
@property
def beats(self):
return self.__class__[self._beats]
def wins_against(self, other):
return {self: 0, self.beats: 1, self.loses: -1}[other]
s, p, r = Choices["scissors"], Choices["paper"], Choices["rock"]
s.wins_against(p) # 1
s.wins_against(s) # 0
s.wins_against(r) # -1
不幸的是,没有很好的方法来丢失其中的抽象(每次调用时将 "paper" 抽象为 Choices.paper)因为你不知道 Choices["paper"]
是什么时候 Choices.rock
被实例化。