简体   繁体   中英

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. 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. 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)

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).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM