Python Forum

Full Version: Grades Code":
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm new to Python and can't figure out what is wrong with this code?

print("please enter your 5 marks below")

#read 5 inputs
mark1 = int(input("enter mark 1: "))
mark2 = int(input("enter mark 2: "))
mark3 = int(input("enter mark 3: "))
mark4 = int(input("enter mark 4: "))
mark5 = int(input("enter mark 5: "))

#create array/list with five marks
marksList = [mark1, mark2, mark3, mark4, mark5]

#print the array/list
print(marksList)

#calculate the sum and average
sumOfMarks = sum(marksList)
averageOfMarks = sum(marksList)/5

#display results
print("The sum of your marks is: "+str(sumOfMarks))
print("The average of your marks is: "+str(averageOfMarks))
Anthony.
What would you like to change? Or are you simply looking for a better way to write that script?

The first thing to note is this: whenever you see repetitive code lines, it's a sure thing that you need a code loop.

There's a few different ways in which this can be coded, but let's stay with the list object. You need a list of five values, so that's what your loop should do:

print("please enter your 5 marks below")

# create an empty array
marksList = []

# populate it
while len(marksList) != 5:
    entry = len(marksList) + 1
    mark = int(input(f"enter mark {entry}: "))
    marksList.append(mark)
From here, well please post back, so that we can move this forward. I have to be FAK for a few hours, but I'm sure that someone else will pick this up. If not, I will pick it up when I get done with other things.
(Nov-22-2023, 03:58 AM)Anthony Wrote: [ -> ]can't figure out what is wrong with this code?
What makes you think that there is something wrong? It can be written way better, but it works
(Nov-22-2023, 07:21 AM)rob101 Wrote: [ -> ]You need a list of five values, so that's what your loop should do:
well, if you have known, finite number of elements, you better use for loop (note, you don't validate the input, i.e. it is not take input until 5 valid inputs)
(Nov-22-2023, 08:09 AM)buran Wrote: [ -> ]well, if you have known, finite number of elements, you better use for loop (note, you don't validate the input, i.e. it is not take input until 5 valid inputs)

Yes, I know a for: loop would be better. I was just tying to introduce one concept at a time, rather than add the range() function into the mix at this stage. Also I know that any input should be validated, but again, for the same reason, I did not introduce that at this stage.
Just for fun:

>>> [int(input(f"Enter mark {i}: ")) for i in range(1, 6)]
Enter mark 1: 10
Enter mark 2: 20
Enter mark 3: 30
Enter mark 4: 40
Enter mark 5: 50
[10, 20, 30, 40, 50]
Anthony.

By using a while: loop, rather than a for: loop, you can make your code a little more flexible, in so much as you can have as few or as many 'marks' as you'd like; simply leave the entry blank to finish entering marks. This script also check that any given mark is between zero and 100, but it does not do any sanity checks, which it should do, but I don't want to overwhelm you.

Whenever I write code, I always consider what may be needed in the future, as well as the here and now.

This is still a basic script, but it should demonstrate some of the principles that you could use when you code other projects.

print("please enter your marks below")

# create an empty array and initialise 'mark'
marksList = []
mark = True

while mark:
    entry = len(marksList) + 1
    mark = input(f"enter mark {entry}: ")
    if mark:
        if int(mark) in range(101):
            marksList.append(int(mark))
        else:
            print("Invalid entry.")
if marksList:
    # display the results
    print("Your marks:")
    print(*marksList)
    print("The sum of your marks is:", sum(marksList))
    print("The average of your marks is:", sum(marksList) / len(marksList))
If you've any questions, then feel free to ask.
Hi Rob,

Thank-you for your reply, Your rewritten code is starting to make sense for me.
What is the small "f' in the brackets for?
Also there is a final part to this problem.

Test data
Table 5: Test data

1. Create another test case using the samples above as a guide.
2. Use debugging and problem-solving techniques to detect and correct errors in the code (use the test cases to confirm the design specifications). This must include examining the contents of the variables. Document the changes you made by including comments in the code, using the correct syntax.
another end part to this,

mark1 mark2 mark3 mark4 mark5 Expected sum Expected average Comments
10 12 16 14 18 70 14
A B D A C n/a n/a Exception error on input
17 17 199 20 - 17 90 18
(Nov-22-2023, 09:53 PM)Anthony Wrote: [ -> ]Thank-you for your reply, Your rewritten code is starting to make sense for me.
What is the small "f' in the brackets for?

You are very welcome and I'm pleased that the code does make sense, as that demonstrates that you're learning, and learning fast: there are concepts there that I'm sure will be new to you, such as "f-strings", which brings me to your question.

When you see a string object that is prefixed with f, that's called an "f-string". What this does, is to tell the python interpreter to insert the value of an object into the string. You can have as many of as few insertion points (AKA: "place holders") as you need, and each point is indicated by a brace pair {} into which you place the object that you want to see when said string is interpreted.

Have a read of this tutorial for a much better explanation than I can provide.

I'll have a look at the rest of your needs and post back when I have the chance. In the mean time you should have a go at solving that for yourself, and post back if you hit a wall.
(Nov-22-2023, 10:30 PM)rob101 Wrote: [ -> ]
(Nov-22-2023, 09:53 PM)Anthony Wrote: [ -> ]Thank-you for your reply, Your rewritten code is starting to make sense for me.
What is the small "f' in the brackets for?

You are very welcome and I'm pleased that the code does make sense, as that demonstrates that you're learning, and learning fast: there are concepts there that I'm sure will be new to you, such as "f-strings", which brings me to your question.

When you see a string object that is prefixed with f, that's called an "f-string". What this does, is to tell the python interpreter to insert the value of an object into the string. You can have as many of as few insertion points (AKA: "place holders") as you need, and each point is indicated by a brace pair {} into which you place the object that you want to see when said string is interpreted.

Have a read of this tutorial for a much better explanation than I can provide.

I'll have a look at the rest of your needs and post back when I have the chance. In the mean time you should have a go at solving that for yourself, and post back if you hit a wall.
Pages: 1 2