我正在尝试制作一个数据记录代码,供用户输入动物的数量和年龄,出生率以及他们在这一代中幸存的机会。我试图使用return但是当我运行程序时,变量保持为0。
这里我已经启动了变量
Gen0_J=0
Gen0_A=0
Gen0_S=0
Birth_rate=0
Srate_J=0
Srate_A=0
Srate_S=0
New_generations=5
我的第一个功能
def generation_values():
Gen0_J=int(input("How many juveniles in Generation 0? "))
Gen0_A=int(input("How many adults in Generation 0? "))
Gen0_S=int(input("How many seniles in Generation 0? "))
Srate_J=float(input("What is the survival rate for juveniles? "))
Srate_A=float(input("What is the survival rate for adults? "))
Srate_S=float(input("What is the survival rate for seniles? "))
Birth_rate=int(input("What is the birth rate? "))
return Gen0_J,Gen0_A,Gen0_S,Srate_J,Srate_A,Srate_S,Birth_rate
显示变量
def display_values():
print("\nThe amount of juveniles in Generation 0 is",Gen0_J)
print("The amount of adults in Generation 0 is",Gen0_A)
print("The amount of seniles in Generation 0 is",Gen0_S)
print("The birth rate in Generation 0 is",Birth_rate)
print("The survival rate for juveniles in Generation 0 is",Srate_J)
print("The survival rate for adults in Generation 0 is",Srate_A)
print("The survival rate for seniles in Generation 0 is",Srate_S)
generation_values()
display_values()
但是,变量保持在0
How many juveniles in Generation 0? 5
How many adults in Generation 0? 6
How many seniles in Generation 0? 7
What is the survival rate for juveniles? 0.75
What is the survival rate for adults? 1
What is the survival rate for seniles? 0
What is the birth rate? 2
The amount of juveniles in Generation 0 is 0
The amount of adults in Generation 0 is 0
The amount of seniles in Generation 0 is 0
The birth rate in Generation 0 is 0
The survival rate for juveniles in Generation 0 is 0
The survival rate for adults in Generation 0 is 0
The survival rate for seniles in Generation 0 is 0
答案 0 :(得分:1)
变量的范围不正确。 [在函数内部,类内部或模块内部声明的变量不能在程序的更高级别访问 - 这里a short description of python scoping rules super_biased_man在评论中发布了 - 在这种情况下,您在generation_values()
中分配的变量是在本地定义的。
这肯定不是正确的方法,但是声明变量是全局的
generation_values()
将解决您的问题:(定义执行此操作的“理想方式”取决于您在编程方面所处的位置,并且会使我们走得太远)
Gen0_J=0
Gen0_A=0
Gen0_S=0
Birth_rate=0
Srate_J=0
Srate_A=0
Srate_S=0
New_generations=5
def generation_values():
global Gen0_J,Gen0_A,Gen0_S,Srate_J,Srate_A,Srate_S,Birth_rate
Gen0_J=int(input("How many juveniles in Generation 0? "))
Gen0_A=int(input("How many adults in Generation 0? "))
Gen0_S=int(input("How many seniles in Generation 0? "))
Srate_J=float(input("What is the survival rate for juveniles? "))
Srate_A=float(input("What is the survival rate for adults? "))
Srate_S=float(input("What is the survival rate for seniles? "))
Birth_rate=int(input("What is the birth rate? "))
return Gen0_J,Gen0_A,Gen0_S,Srate_J,Srate_A,Srate_S,Birth_rate
def display_values():
print("\nThe amount of juveniles in Generation 0 is",Gen0_J)
print("The amount of adults in Generation 0 is",Gen0_A)
print("The amount of seniles in Generation 0 is",Gen0_S)
print("The birth rate in Generation 0 is",Birth_rate)
print("The survival rate for juveniles in Generation 0 is",Srate_J)
print("The survival rate for adults in Generation 0 is",Srate_A)
print("The survival rate for seniles in Generation 0 is",Srate_S)
generation_values()
display_values()
处理这个问题的另一种方法是将变量作为参数传递给函数,并使用赋值的值返回它们......但是它们中有很多并传递它们可能会使代码混乱。 / p>
您也可以将它们收集在一个元组中或将它们包装在一个类中。
答案 1 :(得分:1)
变量的范围不一致。
你可以这样做:
def generation_values():
Gen0_J=int(input("How many juveniles in Generation 0? "))
Gen0_A=int(input("How many adults in Generation 0? "))
Gen0_S=int(input("How many seniles in Generation 0? "))
Srate_J=float(input("What is the survival rate for juveniles? "))
Srate_A=float(input("What is the survival rate for adults? "))
Srate_S=float(input("What is the survival rate for seniles? "))
Birth_rate=int(input("What is the birth rate? "))
return Gen0_J,Gen0_A,Gen0_S,Srate_J,Srate_A,Srate_S,Birth_rate
def display_values(Gen0_J,Gen0_A,Gen0_S,Srate_J,Srate_A,Srate_S,Birth_rate):
print("\nThe amount of juveniles in Generation 0 is",Gen0_J)
print("The amount of adults in Generation 0 is",Gen0_A)
print("The amount of seniles in Generation 0 is",Gen0_S)
print("The birth rate in Generation 0 is",Birth_rate)
print("The survival rate for juveniles in Generation 0 is",Srate_J)
print("The survival rate for adults in Generation 0 is",Srate_A)
print("The survival rate for seniles in Generation 0 is",Srate_S)
Gen0_J,Gen0_A,Gen0_S,Srate_J,Srate_A,Srate_S,Birth_rate = generation_values()
display_values(Gen0_J,Gen0_A,Gen0_S,Srate_J,Srate_A,Srate_S,Birth_rate)
将是第一步。
这样," main"中的变量namespace与函数中的变量分开。
删除重复的下一步可能不是关心主命名空间中变量的分离,只是将它们视为元组:
data = generation_values()
display_values(*data)
删除重复和引入清晰度的其他步骤可包括:
答案 2 :(得分:0)
正如其他人所说,问题在于对函数内部变量的赋值会创建新变量,而不是将值赋给全局变量。这就是global
关键字存在的原因。
在这种情况下,我会考虑使用一个类,因为你有一些你可以一起操作的相关状态值。
class GenerationData(object):
def __init__(self, gen_number):
self.number = gen_number
self.birth_rate = 0
self.num_juveniles, self.num_adults, self.num_seniles = 0, 0, 0
self.juvenile_rate, self.adult_rate, self.senile_rate = 0.0, 0.0, 0.0
@classmethod
def read(cls, gen_number):
gen = cls(gen_number)
gen.num_juveniles = read_integer(
'How many juveniles in Generation {}?'.format(gen_number))
gen.num_adults = read_integer(
'How many adults in Generation {}?'.format(gen_number))
gen.num_seniles = read_integer(
'How many seniles in Generation {}?'.format(gen_number))
gen.juvenile_rate = read_float(
'What is the survival rate for juveniles? ')
gen.adult_rate = read_float('What is the survival rate for adults? ')
gen.senile_rate = read_float('What is the survival rate for seniles? ')
gen.birth_rate = read_integer('What is the birth rate? ')
def display(self):
print('The amount of juveniles in Generation', self.number,
'is', self.num_juveniles)
print('The amount of adults in Generation', self.number,
'is', self.num_adults)
print('The amount of seniles in Generation', self.number,
'is', self.num_seniles)
print('The birth rate in Generation', self.number,
'is', self.birth_rate)
print('The survival rate for juveniles in Generation', self.number,
'is', self.juvenile_rate)
print('The survival rate for adults in Generation', self.number,
'is', self.adult_rate)
print('The survival rate for seniles in Generation', self.number,
'is', self.senile_rate)
def read_integer(prompt):
return int(raw_input(prompt))
def read_float(prompt):
return float(raw_input(prompt))
first_generation = Generation.read(0)
first_generation.display()
当您操作生成数据时,这会使事情变得更容易,因为它们都捆绑在一个对象中。