[英]How does one save a randomly generated variable to the program in Python?
In my code, I have it set up so that it will randomly assign a value from 1-15 to the player.在我的代码中,我对其进行了设置,以便它将随机分配一个 1-15 的值给玩家。 I would like to know how to "save" that same value, so it can be used in other parts of the game to determine events and other stuff throughout the game.
我想知道如何“保存”相同的值,以便可以在游戏的其他部分使用它来确定整个游戏中的事件和其他内容。
FYI earlier in the code I imported tkinter, time, and random.仅供参考,我在前面的代码中导入了 tkinter、时间和随机。 The code below is functional as well.
下面的代码也可以正常工作。
print(name + "now gets different points assigned their characteristics.")
print("Characteristic strength is based out of 15.")
print("i.e. 14/15 INTELLIGENCE")
time.sleep(3)
print(name + "'s INTELLIGENCE, ATHLETIC ABILITY, CREATIVITY, COMMUNICATIONS, and LOGIC.")
time.sleep(3)
print("Intelligence")
print(random.choice(intelligence))
time.sleep(2)
print("Athletic Ability")
print(random.choice(athletic_ability))
time.sleep(2)
print("Creativity")
print(random.choice(creativity))
time.sleep(2)
print("Communications")
print(random.choice(communications))
time.sleep(2)
print("Logic")
print(random.choice(logic))
time.sleep(2)
You can set up a list of number of ranges and then assign it to a value and save it for later use.您可以设置范围数量的列表,然后将其分配给一个值并保存以供以后使用。
import random
intelligence_levels = list(range(1,16,1)) # function range params are start, stop , step where stop number does not included in the list
player_int = random.choice(intelligence_levels)
randomized = []
randomized.append(player_int)
Assign the value to a variable.将值分配给变量。 eg:
some_variable_name = random.choice(creativity)
例如:
some_variable_name = random.choice(creativity)
If you want to store variables locally(at your machine) so that next time you run the program, it still knows the value of variables.如果您想在本地(在您的机器上)存储变量,以便下次运行程序时,它仍然知道变量的值。 I would recommend pickle Or simply store them in a txt file(easy approach).
我会推荐pickle或者只是将它们存储在一个 txt 文件中(简单的方法)。
import json
import random
import time
value_range = range(1, 16)
def player_entry(name):
characteristics = {}
characteristics["Inteligence"] = random.choice(value_range)
characteristics["Athletic Ability"] = random.choice(value_range)
characteristics["Creativity"] = random.choice(value_range)
characteristics["Communications"] = random.choice(value_range)
characteristics["Logic"] = random.choice(value_range)
return characteristics
def add_characteristic(player_characteristic, new_characteristic):
''' add or update new characteristic '''
player_characteristic[new_characteristic] = random.choice(value_range)
return player_characteristic
players_dict = {}
player_name = "Carlos Lewis"
players_dict[player_name] = player_entry(player_name)
print players_dict
list_of_players = ["Paul Eds", "James Ashton", "Ricky Jr."]
for player_name in list_of_players:
players_dict[player_name] = player_entry(player_name)
print json.dumps(players_dict,indent=4)
# Add new characteristc
print "later..."
print "add Attitude"
players_dict["James Ashton"] = add_characteristic(players_dict["James Ashton"], "Attitude")
print json.dumps(players_dict,indent=4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.