Initializing variables in python?

Initializing variables in python?

There are several ways to assign the equal variables.

The easiest one:

grade_1 = grade_2 = grade_3 = average = 0.0

With unpacking:

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

With list comprehension and unpacking:

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

I know you have already accepted another answer, but I think the broader issue needs to addressed – programming style that is suitable to the current language.

Yes, initialization isnt needed in Python, but what you are doing isnt
initialization. It is just an incomplete and erroneous imitation of initialization as practiced in other languages. The important thing about initialization in static typed languages is that you specify the nature of the variables.

In Python, as in other languages, you do need to give variables values before you use them. But giving them values at the start of the function isnt important, and even wrong if the values you give have nothing to do with values they receive later. That isnt initialization, its reuse.

Ill make some notes and corrections to your code:

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 didnt 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 handled 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() doesnt 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
   ....

Initializing variables in python?

The issue is in the line –

grade_1, grade_2, grade_3, average = 0.0

and

fName, lName, ID, converted_ID = 

In python, if the left hand side of the assignment operator has multiple variables, python would try to iterate the right hand side that many times and assign each iterated value to each variable sequentially. The variables grade_1, grade_2, grade_3, average need three 0.0 values to assign to each variable.

You may need something like –

grade_1, grade_2, grade_3, average = [0.0 for _ in range(4)]
fName, lName, ID, converted_ID = [ for _ in range(4)]

Leave a Reply

Your email address will not be published. Required fields are marked *