Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:56:53+00:00 2026-06-01T03:56:53+00:00

I am writing a basic program in Python that prompts the user to enter

  • 0

I am writing a basic program in Python that prompts the user to enter 5 test scores. The program will then convert each test score to a grade point (i.e. 4.0, 3.0, 2.0…), and then afterwards take the average of these numbers.

I’ve assigned each test score their own variable, and I’m feeding them into a for loop as follows:

for num in [score1, score2, score3, score4, score5]:
   if num >= 90
       print('Your test score is a 4.0')
   elif num < 90 and >= 80
   .
   .
   # and so on for each grade point.

Now, this does fine for displaying what each test score is equivalent to grade point wise. However, later in the function I need to calculate the average of each of these grade point values. So, I’d actually like to assign a grade point value to the specific variable passed through the for loop at that time. So, when score1 is passed through the for loop, and the appropriate grade point is determined, how can I actually assign that grade point to score1, and then later for score2 and so on as they are passed through the loop?

I hope that makes the question clear. It would seem silly that Python would not have this kind of capability, because if not you wouldn’t be able to redefine any variable passed through a for loop if it is part of a list that is being passed through.


Not only are variables "in" the list (the list actually only stores values) updated, but the list isn’t updated at all by this process. See Why doesn't assigning to the loop variable modify the original list? How can I assign back to the list in a loop? for that version of the question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:56:54+00:00Added an answer on June 1, 2026 at 3:56 am

    “It would seem silly that Python would not have this kind of capability, because if not you wouldn’t be able to redefine any variable passed through a for loop if it is part of a list that is being passed through.” – That’s how most programming languages work. To allow this capability would be bad because it would create something called side-effects, which make code obtuse.

    Additionally this is a common programming pitfall because you should keep data out of variable names: see http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html (especially the list of similar questions; even if you aren’t dealing with variables names, you are at least trying to deal with the variable namespace). The remedy is to work at “one level higher”: a list or set in this case. This is why your original question is not reasonable. (Some versions of python will let you hack the locals() dictionary, but this is unsupported and undocumented behavior and very poor style.)


    You can however force python to use side-effects like so:

    scores = [99.1, 78.3, etc.]
    for i,score in enumerate(scores):
        scores[i] = int(score)
    

    the above will round scores down in the scores array. The right way to do this however (unless you are working with hundreds of millions of elements) is to recreate the scores array like so:

    scores = [...]
    roundedScores = [int(score) for score in scores]
    

    If you have many things you want to do to a score:

    scores = [..., ..., ...]
    
    def processScores(scores):
        '''Grades on a curve, where top score = 100%'''
        theTopScore = max(scores)
    
        def processScore(score, topScore):
            return 100-topScore+score
    
        newScores = [processScore(s,theTopScore) for s in scores]
        return newScores
    

    sidenote: If you’re doing float calculations, you should from __future__ import division or use python3, or cast to float(...) explicitly.


    If you really want to modify what is passed in, you can pass in a mutable object. The numbers you are passing in are instances of immutable objects, but if for example you had:

    class Score(object):
        def __init__(self, points):
            self.points = points
        def __repr__(self):
            return 'Score({})'.format(self.points)
    
    scores = [Score(i) for i in [99.1, 78.3, ...]]
    for s in scores:
        s.points += 5  # adds 5 points to each score
    

    This would still be a non-functional way to do things, and thus prone to all the issues that side-effects cause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a basic UNIX program that involves processes sending messages to each other.
I am writing an application that reads in a large number of basic user
I'm writing a program for my visual basic class that computes the prices of
I'm writing a basic program that copies a string from an existing text file
I'm writing a program that deciphers sentences, syllables, and words given in a basic
In Python 3.2, I'm writing up a basic menu program, and when the option
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel
I'm writing a basic war-driving program. I have gotten it to loop the command
I'm actually writing an MPI program. This is a basic client / server pattern.
I'm writing a basic crawler that simply caches pages with PHP. All it does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.