我试图让 first_name 和 last_name 组合在一起成为一个新的字典条目。多少取决于有多少人在商店购买。如果我购买 50 名士兵,我得到的总是少于那个。
import random, math
first_name = [ "Emily", "Steve" , "Dave" , "Bob" , "James" , "Jim" , "Jenny" , "Will" , "Ryan" ]
last_name = [ "Wright" , "Kalman" , "Meitzen" , "Cole" , "Robins" , "Harrison" , "Saturn" ]
troops = {}
money = 1000
def shop():
loop = 0
global money, first_name, last_name, troops
while loop == 0 :
print("""
Your Money = {}
(Number , Item , cost)
0, Quit ,N/A
1, Troop, 10
""".format(money))
shopq = int( input("What do you want to buy"))
if shopq == 1:
shopq2 = int( input("How many"))
if shopq2 > money :
print(" You cannot by this many")
else:
print("You can buy that many")
money = money - shopq2
troop_number = 0
while troop_number < shopq2 :
s_name = random.choice(first_name) + " " + random.choice(last_name)
troops[s_name] = 100
troop_number = troop_number + 1
print(troops)
print(" Money = {}".format(money))
elif shopq == 0:
break
class dropship:
def create(self, troops):
troop_number = 0
for k in troops :
troop_number = troop_number + 1
print("troops = {}".format(troop_number))
shop()
x = dropship()
x.create(troops)
输出:
Your Money = 1000
(Number , Item , cost)
0, Quit ,N/A
1, Troop, 10
What do you want to buy1
How many50
You can buy that many
{'Ryan Wright': 100, 'Bob Cole': 100, 'Bob Kalman': 100, 'Will Wright': 100, 'Dave Cole': 100, 'Dave Robins': 100, 'Emily Kalman': 100, 'Jenny Kalman': 100, 'Bob Harrison': 100, 'Emily Wright': 100, 'Will Cole': 100, 'Jim Wright': 100, 'Dave Kalman': 100, 'Dave Wright': 100, 'Bob Meitzen': 100, 'Jenny Wright': 100, 'Jenny Harrison': 100, 'Dave Saturn': 100, 'James Robins': 100, 'Bob Robins': 100, 'Dave Meitzen': 100, 'Steve Wright': 100, 'Bob Wright': 100, 'Steve Kalman': 100, 'Ryan Harrison': 100, 'Jim Saturn': 100, 'Steve Robins': 100, 'Ryan Cole': 100, 'Jim Meitzen': 100, 'James Cole': 100, 'Emily Cole': 100, 'Ryan Saturn': 100, 'Steve Harrison': 100}
Money = 950
Your Money = 950
(Number , Item , cost)
0, Quit ,N/A
1, Troop, 10
What do you want to buy0
troops = 33