在整个游戏过程中使用Python制作库存记忆输入



我通过使用Python重新制作一款名为Dope Wars的游戏来练习编程。如果你不熟悉毒品战争,长话短说-你从一个城市到另一个城市买卖毒品。他们的目标是在不被杀害或逮捕的情况下赚尽可能多的钱。我开始制作游戏的命令行,并将其编码成GUI时,我完成。我发布的代码有2个城市可用和2种药物购买。命令行询问你想去哪个城市,如果你想买,你想买什么,买多少。我得到它来更新我存储值的字典,但它不保存值作为你的循环,我很确定这是因为使用。update(),但我不知道还有什么可以使用。

所以我基本上是在寻找一种方法来保持库存的价值,这样他们就可以添加和提取,购买和出售。我还需要建立一个货币系统,随机警察追捕,银行,医院和枪支商店,任何在这些方面的帮助也将大有帮助。

到目前为止我写的是:

import random
#  Stores dope list with prices for each city
nyDope = {'Heroin': random.randrange(1000, 2000), 'Coke': random.randrange(1000, 2000)}
laDope = {'Coke': random.randrange(1000, 2000)}
# Stores references for cities to their dope list
cityDict = {'New York': nyDope, 'Los Angeles': laDope}

# to print out dope list for NY
def ny():
    for i in nyDope:
        print(i + ': ' + str(nyDope[i]))

# To print out dope list for LA
def la():
    for i in laDope:
        print(i + ': ' + str(laDope[i]))
print('Pick your city')
city1 = input()
if city1.lower().startswith('n'):
    ny()
elif city1.lower().startswith('l'):
    la()
else:
    print('Wrong answer, now you have to go to New York')
    ny()
goAgain = True
unitsHeroin = 0
unitsCoke = 0
while goAgain == True:
    inventory = {'Heroin': int(unitsHeroin), 'Coke': int(unitsCoke)}
    print('Type what drug you want to buy:')
    buyDrugs1 = input()
    if buyDrugs1.lower() == 'heroin':
        print('How many?')
        boughtDrugs1 = input()
    elif buyDrugs1.lower() == 'coke':
        print('How many?')
        boughtDrugs1 = input()
    else:
        print("That's not an option")
    if buyDrugs1.lower() == 'heroin':
        inventory.update({'Heroin': int(unitsHeroin) + int(boughtDrugs1)})
    if buyDrugs1.lower() == 'coke':
        inventory.update({'Coke': int(unitsCoke) + int(boughtDrugs1)})
    print('Heroin: ' + str(inventory['Heroin']), 'nCoke: ' + str(inventory['Coke']))
    print('Buy more drugs? y/n')
    goAgain = input()
    if goAgain.lower().startswith('y'):
        goAgain = True
    else:
        goAgain = False
        break

在每次迭代开始时初始化inventory;将初始化移到主循环开始之前。或者,您可以更改units变量,以便在重新创建inventory时,它使用它们的新值。

更新后的代码:

import random
#  Stores dope list with prices for each city
nyDope = {'Heroin': random.randrange(1000, 2000), 'Coke': random.randrange(1000, 2000)}
laDope = {'Coke': random.randrange(1000, 2000)}
# Stores references for cities to their dope list
cityDict = {'New York': nyDope, 'Los Angeles': laDope}

# to print out dope list for NY
def ny():
    for i in nyDope:
        print(i + ': ' + str(nyDope[i]))

# To print out dope list for LA
def la():
    for i in laDope:
        print(i + ': ' + str(laDope[i]))
print('Pick your city')
city1 = input()
if city1.lower().startswith('n'):
    ny()
elif city1.lower().startswith('l'):
    la()
else:
    print('Wrong answer, now you have to go to New York')
    ny()
goAgain = True
unitsHeroin = 0
unitsCoke = 0
inventory = {'Heroin': int(unitsHeroin), 'Coke': int(unitsCoke)}
while goAgain == True:

    print('Type what drug you want to buy:')
    buyDrugs1 = input()
    if buyDrugs1.lower() == 'heroin':
        print('How many?')
        boughtDrugs1 = input()
    elif buyDrugs1.lower() == 'coke':
        print('How many?')
        boughtDrugs1 = input()
    else:
        print("That's not an option")
    if buyDrugs1.lower() == 'heroin':
        inventory['Heroin'] = int(inventory['Heroin']) + int(boughtDrugs1)
    if buyDrugs1.lower() == 'coke':
        inventory['Coke'] = int(inventory['Coke']) + int(boughtDrugs1)
    print('Heroin: ' + str(inventory['Heroin']), 'nCoke: ' + str(inventory['Coke']))
    print('Buy more drugs? y/n')
    goAgain = input()
    if goAgain.lower().startswith('y'):
        goAgain = True
    else:
        goAgain = False
        break

相关内容

  • 没有找到相关文章

最新更新