我如何在另一个函数中调用一个函数?
how can i call one function inside of another?
我只想在骰子 class 中创建一个单独的函数,这样我就可以将每个 'roll' 存储在 'rolls' 函数的 list_of_rolls 列表中。所以当 'rolls' 被调用时,它会显示每个 'roll' 执行的列表(如果有的话)。
我试过使用 global 但它没有用(也许我做错了),我也听说使用 global 是一个坏习惯所以如果有其他方法我不介意。我的缩进是正确的,只是这里没有显示。
import random
class Dice:
def roll(self):
x = random.randint(1, 6)
y = random.randint(1, 6)
roll_list = (x, y)
return roll_list
def rolls(self):
list_of_rolls = []
final = list_of_rolls.append()
return final
将 list_of_rolls 声明为 class 的成员变量,而不是在函数中定义它。创建一个构造函数来初始化它。如果您在 class 名称之后执行此操作,那么它将变成 class 而不是实例级别。
import random
class Dice:
# list_of_rolls = [] # becomes class variable and dont use it
def __init__(self):
self.list_of_rolls = []
def roll(self):
有几种方法可以做到这一点。然而,我只是建议最直接的方法,即使用文本文件将您的掷骰历史存储在 Dice class 本身中。
请注意,缺点是 Dice 的多个实例将访问同一个历史文件
但是,此实现可能未优化,因为每次掷骰子时都会打开文件并向其添加新的掷骰。如果您需要数百万卷,它可能并不理想。那就是说我会留给你 better/optimize 解决方案。
import random
class Dice:
list_of_rolls = []
filename = "./roll_history.txt" # a textfile to store history of rolls
def __init__(self):
try: # just to check if file exists if not create one for storing
file = open(self.filename, "r")
except FileNotFoundError:
print("File not found")
file = open(self.filename, "x") #creates file
finally:
file.close()
with open(self.filename, 'r') as opened_file_object:
self.list_of_rolls = opened_file_object.read().splitlines()
print(self.list_of_rolls)
def roll(self):
x = random.randint(1, 6)
y = random.randint(1, 6)
roll_list = (x, y)
self.list_of_rolls.append(roll_list) # updates the array with latest roll
file = open(self.filename, 'a') # 'a' for appending new rolls
# I append a newline so that the rolls are more readable in the text file
file.write('(' + str(x) + ',' + str(y) + ')\n') # appends a newline
return roll_list
def rolls(self):
return self.list_of_rolls
print(Dice().roll()) # append 2 dice rolls here
print(Dice().roll())
print(Dice().rolls()) # you should see 2 dice rolls here
尝试关闭您的 python 程序并再次 运行,
Dice() # you should be able to see past rolls
我只想在骰子 class 中创建一个单独的函数,这样我就可以将每个 'roll' 存储在 'rolls' 函数的 list_of_rolls 列表中。所以当 'rolls' 被调用时,它会显示每个 'roll' 执行的列表(如果有的话)。
我试过使用 global 但它没有用(也许我做错了),我也听说使用 global 是一个坏习惯所以如果有其他方法我不介意。我的缩进是正确的,只是这里没有显示。
import random
class Dice:
def roll(self):
x = random.randint(1, 6)
y = random.randint(1, 6)
roll_list = (x, y)
return roll_list
def rolls(self):
list_of_rolls = []
final = list_of_rolls.append()
return final
将 list_of_rolls 声明为 class 的成员变量,而不是在函数中定义它。创建一个构造函数来初始化它。如果您在 class 名称之后执行此操作,那么它将变成 class 而不是实例级别。
import random
class Dice:
# list_of_rolls = [] # becomes class variable and dont use it
def __init__(self):
self.list_of_rolls = []
def roll(self):
有几种方法可以做到这一点。然而,我只是建议最直接的方法,即使用文本文件将您的掷骰历史存储在 Dice class 本身中。 请注意,缺点是 Dice 的多个实例将访问同一个历史文件 但是,此实现可能未优化,因为每次掷骰子时都会打开文件并向其添加新的掷骰。如果您需要数百万卷,它可能并不理想。那就是说我会留给你 better/optimize 解决方案。
import random
class Dice:
list_of_rolls = []
filename = "./roll_history.txt" # a textfile to store history of rolls
def __init__(self):
try: # just to check if file exists if not create one for storing
file = open(self.filename, "r")
except FileNotFoundError:
print("File not found")
file = open(self.filename, "x") #creates file
finally:
file.close()
with open(self.filename, 'r') as opened_file_object:
self.list_of_rolls = opened_file_object.read().splitlines()
print(self.list_of_rolls)
def roll(self):
x = random.randint(1, 6)
y = random.randint(1, 6)
roll_list = (x, y)
self.list_of_rolls.append(roll_list) # updates the array with latest roll
file = open(self.filename, 'a') # 'a' for appending new rolls
# I append a newline so that the rolls are more readable in the text file
file.write('(' + str(x) + ',' + str(y) + ')\n') # appends a newline
return roll_list
def rolls(self):
return self.list_of_rolls
print(Dice().roll()) # append 2 dice rolls here
print(Dice().roll())
print(Dice().rolls()) # you should see 2 dice rolls here
尝试关闭您的 python 程序并再次 运行,
Dice() # you should be able to see past rolls