#๐Ÿ”’ total of mark thing thing

42 messages ยท Page 1 of 1 (latest)

wraith copper
#

Hello, so I made a code to find calculate the average total mark from the user's inputs. So they choose how much marks they want to input but like when a number less than 0 or more than 100, it still counts for one of the inputs, but doesn't add it for the total mark average. For example if I say I want to input 10 different marks, and I input a number like -5, it counts for one of the different marks and I have 9 inputs left. Please help.

Initialize variables

totalMarks = 0
validMarks = 0

Prompt user for number of marks

numMarks = int(input("Enter the number of marks: "))

If number of marks is less than 0, print error message

if numMarks <= 0:
print("Please enter valid mark number of marks")
else:

Input marks and calculate total

for mark in range(numMarks):
mark = float(input("Enter mark: "))
if 0 <= mark <= 100:
# Add the mark to the total
totalMarks += mark
# Add the number of valid marks
validMarks += 1
else:
print("Error: Invalid mark. Please enter a mark between 0 and 100.")

if validMarks <= 0:
print("No valid marks entered.")
else:
average = totalMarks / validMarks
print("The average mark is: {:.1f}%".format(average))

small kettleBOT
#

@wraith copper

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

drifting python
#

is this question about limiting the range of user_input to be in valid range?

wraith copper
#

Create a program to calculate the average of a set of marks. The program will first ask for the number of marks, then ask for each mark. Lastly, it will display the average, to 1 decimal place. Use a for loop.

#

This was the question

#

numMarks = int(input("Enter the number of marks: "))

#

for this

#

like i said in the example

#

i input 10 for 10 different marks

drifting python
#

uhh I don't see a problem here

#

you will have to explain where the problem is

wraith copper
#

uh let me

#

get a screenshot

#

so like

#

i want to enter 5 different marks

#

but enters only 4

#

since i entered a negative number

drifting python
#

so you want the negative number to not count as a mark

wraith copper
#

yeah

#

basically

#

so that I could enter the total amount of marks i want

drifting python
#

because you are limited to for loops (I would use a while loop),
you can check in your for loop if your mark is in valid range
and if invalid, ask for the input again until it's valid

#

(which needs a while loop)

wraith copper
#

is there another way

#

teach don't want us using while loop

#

weird ash

drifting python
#

not at all?

wraith copper
#

yeah he wants to focus on for loop for this one

#

idk man

drifting python
#

well I don't have another idea

wraith copper
#

man

#

ok how would i use the while loop on this

drifting python
#

the first way is to replace the for mark in range(numMarks): with while validMarks != numMarks:

wraith copper
#

oh sorry my bad, i meant like is there a way to incorporate a while loop with a for loop

#

for this

drifting python
#

the second way is after asking for input,

mark = float(input("Enter mark: "))
  while mark > 100 or mark < 0: #if the mark is in valid range, this ckeck will pass:
    print("Error: Invalid mark. Please enter a mark between 0 and 100.")
    mark = float(input("Enter mark: ")) #if it didn't pass, ask for input again
  #continue with code here
#

and with this method you can remove the if 0 <= mark <= 100:

#

but if you are not allowed to use the second method as well you probably wouldn't have to deal with 'negative mark' issue

wraith copper
#

ok, tysm for the help man

small kettleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.