Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将随机生成的变量保存到Python程序中?_Python_Variables_Save - Fatal编程技术网

如何将随机生成的变量保存到Python程序中?

如何将随机生成的变量保存到Python程序中?,python,variables,save,Python,Variables,Save,在我的代码中,我对它进行了设置,以便它将随机为玩家分配1-15之间的值。我想知道如何“保存”相同的值,以便在游戏的其他部分使用它来确定整个游戏中的事件和其他内容 仅供参考,在我之前导入的代码中,tkinter、time和random。下面的代码也是功能性的 print(name + "now gets different points assigned their characteristics.") print("Characteristic strength is based out of

在我的代码中,我对它进行了设置,以便它将随机为玩家分配1-15之间的值。我想知道如何“保存”相同的值,以便在游戏的其他部分使用它来确定整个游戏中的事件和其他内容

仅供参考,在我之前导入的代码中,tkinter、time和random。下面的代码也是功能性的

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)

您可以设置一个范围数列表,然后将其指定给一个值并保存以供以后使用

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)

将值赋给变量。 例如:
some\u variable\u name=random.choice(创造性)

如果您想在本地(在您的机器上)存储变量,以便下次运行程序时,它仍然知道变量的值。我推荐
或者简单地将它们存储在txt文件中(简单方法)。

intelligence=random.randint(1,15)
等等?
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)