Initialization of variables in python?

Although initialization of variables in python is not required, my professor still wants us to do this for practice. I wrote my program and it worked fine, but after I tried to initialize some of the variables, I received an error message when I tried to start it. Here is the first part of my program:

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 

Error message:

 grade_1, grade_2, grade_3, average = 0.0 TypeError: 'float' object is not iterable 
+6
source share
7 answers

The problem is in the line -

 grade_1, grade_2, grade_3, average = 0.0 

and

 fName, lName, ID, converted_ID = "" 

In python, if the left side of an assignment operator has several elements that need to be highlighted, python will try to iterate the right side sequentially and assign each iterated value to each variable sequentially.

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)] 
+10
source

There are several ways to assign equal variables.

Simplest:

 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 a list 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 
+14
source

I know that you already accepted a different answer, but I think that it is necessary to solve a wider problem - a programming style suitable for the current language.

Yes, "initialization" is not required in Python, but what you do is not initialization. This is simply an incomplete and erroneous imitation of initialization, as is practiced in other languages. An important feature of initialization in static typed languages ​​is that you specify the nature of the variables.

In Python, as in other languages, you need to specify the values ​​of variables before using them. But giving them values ​​at the beginning of the function is not important, and it’s even wrong if the values ​​you give have nothing to do with the values ​​that they get later. This is not "initialization", it is "reuse".

I will make some comments 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 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 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() 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 .... 
+9
source

Python treats the comma on the left side of the equal sign ( = ) as an input splitter. Very useful for functions that return a tuple.

eg,

 x,y = (5,2) 

What do you want to do:

 grade_1 = grade_2 = grade_3 = average = 0.0 

although this may not be the clearest way to record it.

+5
source

You are requesting the initialization of four variables with a single float, which, of course, is not iterable. You can do -

  • grade_1, grade_2, grade_3, grade_4 = [0.0 for _ in range(4)]
  • grade_1 = grade_2 = grade_3 = grade_4 = 0.0

If you do not want to initialize them with different values, of course.

+3
source

If you want to use a destructuring assignment, you will need the same number of floats as you have variables:

 grade_1, grade_2, grade_3, average = 0.0, 0.0, 0.0, 0.0 
+2
source
 def grade(inlist): grade_1, grade_2, grade_3, average =inlist print (grade_1) print (grade_2) mark=[1,2,3,4] grade(mark) 
0
source

Source: https://habr.com/ru/post/989170/


All Articles