I want to average out of infinite loop

Asked 2 years ago, Updated 2 years ago, 15 views

Problem
Use while to create an infinite loop.
Enter your score and ask them to enter your test score (integer value).
The average score is calculated by the total number of points/number of people, so we will add the entered score to the total value.
When -1 is entered, repeat the process there.
Lastly, please indicate "The average score on the test for ○○ people is △△."

My own code

n=1
while True:
    score=int(input("Please enter a score""))
    n + = 1
    scores= 
    ave=score/n
    if score==-1:
        break
print(ave)

It's not going well.
I would appreciate it if you could point it out.

python

2022-09-30 16:45

1 Answers

This seems to be an assignment (homework?), so please review the following regarding your thinking.

  • Some variables require initialization but are not.(Think about whether the initialization value should be the same.)
  • Think about what happens if you enter -1 without entering any valid data.
  • Regarding the lack of description of the problem itself (although it is common sense), is a negative value other than -1 valid in the first place?
  • I'm trying to shorten the process, or I'm trying to put something together, and it's a strange calculation.
    Read the problem carefully and do only what you need to do where you need to.

By the way, if you compare the question sentence with the source code, you can't do well in the "average score/number of people" part when you try to convert the contents of the question sentence into a program in the order written in the question sentence.

Since you feel that it has not yet been completed and has not yet reached the point of operation, have you not presented any errors?

The problem statement does not work that way because it bulletifies what the program should do and does not even show detailed execution procedures (so to speak, flowcharts).

For classes/lectures/exercises, you should have presented in advance what to think about and how to work on the program based on the questions/challenges described in these sentences, so please review them and follow them.If you're self-taught, look for reference books and materials about these things and study them.

For example, you'll need to think about this

  • Extract what data is being handled in the issue
  • Consider the characteristics of the extracted data (input/work/output classification, maximum/minimum, duration of survival, etc.)
  • Separate the work that should be done in the problem to a minimum unit that can no longer be broken down
  • Identify the order of execution (before and after) required to achieve the problematic content for each decomposed task


2022-09-30 16:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.