Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loops
#1
I'm completely new to Python and coding in general. I've been asked to write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1. Any help would be appreciated.
Reply
#2
Please make an effort at writing the code.
Then post your code, working or not, with any error messages, and your question.

Please read: Homework and No Effort Questions
Reply
#3
(Nov-24-2022, 10:08 PM)CJM125 Wrote: I'm completely new to Python and coding in general. I've been asked to write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1. Any help would be appreciated.

You could use a while loop and a for loop.
Yoriz write Nov-25-2022, 08:50 AM:
The code you supplied has been removed from your post because of the following forum rule.
https://python-forum.io/misc.php?action=help&hid=52 Wrote:Homework and No Effort Questions

This forum is focused on education. It exists to help people learn Python. We don’t exist to solve others’ problems, although that tends to be a happy byproduct of education. You should keep this in mind when writing replies.

It is highly advised that you do not post the full solution, even if the asker is putting in the effort. It is better to use hints with natural language rather than code if at all possible. Only after the author has something complete you may go all out.
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#4
(Nov-24-2022, 10:08 PM)CJM125 Wrote: I'm completely new to Python and coding in general. I've been asked to write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1. Any help would be appreciated.

One approach would be to write some pseudocode:

# get the user input
# is the input -1
# no:
    # store the input and loop back to get another input
# yes:
   # stop asking for input and process the list of inputs
So, you need to know how to get some user input and check the requirement; that's your first step.

Have you been asked to do this, with no tuition, or have you been given some tuition?

Have a look at this site for, not only the fundamentals, but also more advanced topics.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
And in the while loop, the walrus operator is ideal.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020