如何在Python 3中将输入字符串转换为变量?

时间:2019-01-12 18:07:49

标签: python python-3.x

我正在创建Dominion纸牌游戏,但我坚持如何将玩家输入的字符串转换为变量。例如,如果玩家1在其行动阶段想使用自己的地窖卡,他将输入“地窖”。但是王国卡的类型很多,我认为编写if语句来检查所有不同的王国卡名称是正确的。有没有一种方法可以将输入字符串“ cellar”转换为可以直接调用的变量?在这种情况下,我将其作为功能。谢谢!

import random

# Need dictionary of each card: Cost, action, points, curse, etc.

# BASIC CARDS
copper = {"name":"copper", "cardtype":"treasure", "victory":0, "cost":0, "value":1}
silver = {"name":"silver", "cardtype":"treasure", "victory":0, "cost":3, "value":2}
gold = {"name":"gold", "cardtype":"treasure", "victory":0, "cost":6, "value":3}

estate = {"name":"estate", "cardtype":"victory", "victory":1, "cost":2, "value":0}
duchy = {"name":"duchuy", "cardtype":"victory", "victory":3, "cost":5, "value":0}
province = {"name":"province", "cardtype":"victory", "victory":6, "cost":8, "value":0}
curse = {"name":"curse", "cardtype":"victory", "victory":-1, "cost":0, "value":0}

# KINGDOM CARDS
def cellar(x):
    pass

# Draw pile, discard pile, hand pile.

drawpile = [0]
discardpile = [0]
hand = [0]

# Each player starts with 8 copper and 2 estates. Shuffle cards and put in draw pile.
number_of_players = input("Ready to start? Input # of players")
if int(number_of_players) < 2:
    print("Need at least 2 players")
elif int(number_of_players) > 8:
    print("8 is maximum")
else:
    for i in range(int(number_of_players)):
        print("Player " + str(i))

# Player 1 starts, draw 5 cards from draw pile to hand.
drawpile = [copper["name"], copper["name"], copper["name"], copper["name"], copper["name"], copper["name"], copper["name"], estate["name"], estate["name"]]
random.shuffle(drawpile)
hand = drawpile[:6]
print(hand)
turn1 = input("Action phase, what would you like to do: ")
turn2 = input("Buy phase, what would you like to buy: ")
turn3 = input("Cleanup phase: ")

0 个答案:

没有答案