"初始化" python中的变量?

时间:2015-06-16 03:43:21

标签: python python-3.x

即使在python中初始化变量不是必要的,我的教授仍然希望我们为实践做这件事。我写了我的程序,它工作正常,但在我尝试初始化一些变量后,当我尝试运行它时,我收到了一条错误消息。这是我的计划的第一部分:

def main():

    grade_1, grade_2, grade_3, average = 0.0
    year = 0

    fName, lName, ID, converted_ID = ""
    infile = open("studentinfo.txt", "r")
    data = infile.read()
    fName, lName, ID, year = data.split(",")
    year = int(year)

    # Prompt the user for three test scores

    grades = eval(input("Enter the three test scores separated by a comma: "))

    # Create a username

    uName = (lName[:4] + fName[:2] + str(year)).lower()
    converted_id = ID[:3] + "-" + ID[3:5] + "-" + ID[5:]
    grade_1, grade_2, grade_3 = grades

错误消息:

grade_1, grade_2, grade_3, average = 0.0

TypeError: 'float' object is not iterable

7 个答案:

答案 0 :(得分:14)

有几种方法可以分配相等的变量。

最简单的一个:

grade_1 = grade_2 = grade_3 = average = 0.0

解压缩:

grade_1, grade_2, grade_3, average = 0.0, 0.0, 0.0, 0.0

使用列表理解和解包:

>>> grade_1, grade_2, grade_3, average = [0.0 for _ in range(4)]
>>> print(grade_1, grade_2, grade_3, average)
0.0 0.0 0.0 0.0

答案 1 :(得分:10)

问题在于 -

grade_1, grade_2, grade_3, average = 0.0

fName, lName, ID, converted_ID = ""

在python中,如果赋值运算符的左侧有多个要分配的项,python会尝试多次迭代右侧,并按顺序将每个迭代值分配给每个变量。

你可能需要像 -

这样的东西
grade_1, grade_2, grade_3, average = [0.0 for _ in range(4)]
fName, lName, ID, converted_ID = ["" for _ in range(4)]

答案 2 :(得分:8)

我知道你已经接受了另一个答案,但我认为需要解决更广泛的问题 - 适合当前语言的编程风格。

是的,Python中不需要'初始化',但你所做的不是 初始化。它只是对其他语言中实践的初始化的不完整和错误的模仿。在静态类型语言中初始化的重要一点是指定变量的性质。

在Python中,与其他语言一样,您需要在使用变量值之前给出它们。但是在函数开始时给它们赋值并不重要,如果你给出的值与它们后来收到的值无关,那就更糟了。这不是'初始化',而是'重用'。

我会对您的代码做一些注释和更正:

def main():
   # doc to define the function
   # proper Python indentation
   # document significant variables, especially inputs and outputs
   # grade_1, grade_2, grade_3, average - id these
   # year - id this
   # fName, lName, ID, converted_ID 

   infile = open("studentinfo.txt", "r") 
   # you didn't 'intialize' this variable

   data = infile.read()  
   # nor this  

   fName, lName, ID, year = data.split(",")
   # this will produce an error if the file does not have the right number of strings
   # 'year' is now a string, even though you 'initialized' it as 0

   year = int(year)
   # now 'year' is an integer
   # a language that requires initialization would have raised an error
   # over this switch in type of this variable.

   # Prompt the user for three test scores
   grades = eval(input("Enter the three test scores separated by a comma: "))
   # 'eval' ouch!
   # you could have handle the input just like you did the file input

   grade_1, grade_2, grade_3 = grades   
   # this would work only if the user gave you an 'iterable' with 3 values
   # eval() doesn't ensure that it is an iterable
   # and it does not ensure that the values are numbers. 
   # What would happen with this user input: "'one','two','three',4"?

   # Create a username 
   uName = (lName[:4] + fName[:2] + str(year)).lower()

   converted_id = ID[:3] + "-" + ID[3:5] + "-" + ID[5:]
   # earlier you 'initialized' converted_ID
   # initialization in a static typed language would have caught this typo
   # pseudo-initialization in Python does not catch typos
   ....

答案 3 :(得分:5)

Python将等号左侧的逗号(=)视为输入拆分器, 对于返回元组的函数非常有用。

e.g,

x,y = (5,2)

您想要做的是:

grade_1 = grade_2 = grade_3 = average = 0.0

虽然这可能不是最清晰的写作方式。

答案 4 :(得分:3)

您要求使用单个float对象初始化四个变量,这当然不可迭代。你可以做 -

  1. grade_1, grade_2, grade_3, grade_4 = [0.0 for _ in range(4)]
  2. grade_1 = grade_2 = grade_3 = grade_4 = 0.0
  3. 除非你想用不同的值初始化它们。

答案 5 :(得分:2)

如果你想使用解构赋值,你需要与变量相同数量的浮点数:

adapter.notifyDataSetChanged();

答案 6 :(得分:0)

def grade(inlist):
    grade_1, grade_2, grade_3, average =inlist
    print (grade_1)
    print (grade_2)

mark=[1,2,3,4]
grade(mark)