I have been learning python for 2 days. So I am making this program which generates different numbers, and adds them to a string. I have a function for getting the random numbers:
def GetRandomNumbers():
random_number = random.choice(zero_to_nine)
two_random_number = random.choice(zero_to_nine)
three_random_number = random.choice(zero_to_nine)
four_random_number = random.choice(zero_to_nine)
//
def SettingVariables():
first_combination = ("__asm _emit " + zero + ("x") + random_number + " " + "/")
two_first_combination = ("__asm _emit " + zero + ("x") + two_random_number + " " + "/")
three_first_combination = ("__asm _emit " + zero + ("x") + three_random_number + " " + "/")
four_first_combination = ("__asm _emit " + zero + ("x") + four_random_number + " " + "/")
And then when I attempt to use theese generated variables in a different function where theese numbers are added onto text it is unidentified. I know it has something to do with global variables, but I would need some explaining.
Propably you should return values from your first function:
def GetRandomNumbers():
... # get your numbers
return random_number, two_random_number, ...
Then you can parse these numbers as an argument to your second function:
def SettingVariables(first_num, second_num, third_num): ...
You could set a global variable like this (this is not recommended):
global var
var = 1
Your code could look like this:
def GetRandomNumbers():
random_number = random.choice(zero_to_nine)
two_random_number = random.choice(zero_to_nine)
three_random_number = random.choice(zero_to_nine)
four_random_number = random.choice(zero_to_nine)
return random_number, two_random_number, three_random_number, four_random_number
def SettingVariables(random_number, two_random_number, three_random_number, four_random_number):
first_combination = ("__asm _emit " + zero + ("x") + random_number + " " + "/")
two_first_combination = ("__asm _emit " + zero + ("x") + two_random_number + " " + "/")
three_first_combination = ("__asm _emit " + zero + ("x") + three_random_number + " " + "/")
four_first_combination = ("__asm _emit " + zero + ("x") + four_random_number + " " + "/")
# later in the program
nums = GetRandomNumbers()
SettingVariables(nums[0], nums[1], nums[2], nums[3]) # could also use *nums
Note that your SettingVariables function should also return the "combinations". You then can use the same principle
It's interesting that you're function names contains keyword get
and set
. These are concept familiar to classes. You may consider define some classes here. An example would be
class RandomNumbers:
def __init__(self, zero_to_nine):
random_number = random.choice(zero_to_nine)
class Variables:
def __init__(self, random_numbers):
self.first_combination = ("__asm _emit " + zero + ("x") + random_numbers.random_number + " " + "/")
...
Then you can do like this:
random_numbers = RandomNumbers()
variables = Variables(random_numbers)
print(variables.first_combination)
[edit]
I suggested classes as it seems that you'are using functions as initializers. They are quite similar to what in OOP are object constructors.
This would avoid the need for declaring all a bunch of variables outside of the function, update them an returning from the function.
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.