On Thu, 18 Mar 2021 23:42:04 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user Ekkam Singh
<ekkhanuja-re5jqeeqqe8avxtiumw...@public.gmane.org> wrote:

>hi 
>i am writing a code in python but it is coming error when i write the 
>program :-
>for line in username:
>                content = username.split(' , ')

                        content = line.split(",")

        NOTE: your split is explicitly expecting <space><comma><space> which is
an unnatural usage of commas. Better, I would think, is to use
content[*].strip() to get rid of the spaces after splitting on the comma.

>                if content[0] == userName:
>                                content[1] = score
>                                line = content[0] + ', ' + content[1] + '\n'

        Confusing usage. Your main input is a list or iterable called
"username", yet you are expecting the first term of each LINE of that
iterable to contain something you compare to whatever userName contains.

        Also, note that you are appending a newline when recreating line, but
does the unmodified line end with a newline? (I'm assuming you later write
either the modified or unmodified line to some file).

        There is no need to bind "score" to content[1] if the only usage is
then to join strings. Also, "score" needs to be a string for that joining
to work -- if it is numeric you need to convert it. Consider:

        for line in username:   #still don't like that object name...
                                                #user_scores may be more 
applicable.
                uName = line.split(",")[0].strip()      #don't care about rest
                if uName == userName:
                        line = "%s, %s\n" % (uName, score)
                        #using %s means whatever "score" type, it will
                        #be converted to a string representation

 


-- 
Dennis L Bieber

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to beagleboard+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beagleboard/tg7a5gp6hvmoltbr1klop0kugovngg6j9k%404ax.com.

Reply via email to