我通过使用Python重新制作一个名为Dope Wars的游戏来练习我的编码。如果你不熟悉Dope Wars,长话短说 - 你从一个城市到另一个城市买卖毒品。目标是赚取尽可能多的钱,而不是被杀或被捕。我开始让游戏通过命令行播放,并在完成后将其编码到GUI中。代码Im发布有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
答案 0 :(得分:0)
您在每次迭代开始时初始化inventory
;在主循环开始之前将初始化移动到。或者,您可以对units
变量进行更改,以便在重新创建inventory
时,它会使用新值。
答案 1 :(得分:0)
这是您更新的代码:
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