help with

I’ve got the logic and the flowchart but I think I mess with the syntax.

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.

**Make use of the while loop repetition structure to implement the program.**

Hello, could you please share (at least) your logic, and possibly the flowchart as well? That way we have a starting point to work from.

Hi…

well the logic is…

print(input("enter a number: "))

if the number is different then -1 should go back and ask again to enter another number

so that would be the … while loop

while x != -1

???

if the input is -1 then will break the loop and print the average

which would be

x = x/len(x)

???/

print(input("number: "))

count=0

sum=0

while (count != (-1)):

input (“number”)

print(‘average of list’‘is’,(sum/len(count)))

I’m not very good on explaining myself so, please be patient

Formatting this to python real quick…

```
x = input("enter a number: ")
# if the number is different then -1 should go back and ask again to enter another number
# so that would be the … while loop
while x != -1
# ???
# if the input is -1 then will break the loop and print the average
# which would be
x = x/len(x) # Exp
# ???/
x = input("number: ") # Exp and two lines below
count=0
sum=0
while (count != -1): # Infinite loop
x = input("number")
print('average of list","is",(sum/len(count)))
```

If possible please explain the lines that I added `# Exp`

to.

count=0

sum=0

a= input("number: ")

while a != -1:

print(a)

count += 1

else:

print(‘average of list’‘is’,(sum/len(count)))

this is where I am, sorry im not very helpfull

i tried to start from scratch a few times but I always end with the same code and I don’t understand why. At this point I’m not sure if I need to declare those variables or no.

I found different examples to calculate the average of a list of numbers but none for the average of a list of introduced numbers

count=0

sum=0

a= input("number: ")

while (a != (-1)):

print(a)

else:

print(‘average of list’‘is’,(sum/len(count)))

this is the first example I checked and tried to start from

a=[19, 25, 74, 38, 99, 39, 18, 83, 10, 50]

count=0

sum=0

while (count<len(a)):

sum=sum+a[count]

count=count+1

print(‘average of list’,a,‘is’,(sum/len(a)))

Please format your code like this:

Get this result:

```
print("Hello world")
```

Using this method:

```python

print("Hello world")

```

this is giving me the average of the list of numbers in “a”

and it says, while count is less then the numbers on the list add 1 on count so it will keep going for 10 times and then will calculate the average.

a=[19, 25, 74, 38, 99, 39, 18, 83, 10, 50]

count=0

sum=0

while (count<len(a)):

```
```count=count+1
print('average of list',a,'is',(sum/len(a)))
what i dont understand is If I declare a variable like
x = print(input("enter a number: "))
will x be a string of caracters or numbers
```

Taking a guess at what you want here…

```
while x != -1
x = int(input("Input the first number: "))
count=0
total=0
numberList = [x]
while (x != -1): # Infinite loop
x = int(input("Input the next number (-1 to exit): "))
total += x
numberList.append(x)
print("Average of the list","is",(total/len(numberList)))
```

I’m fairly certain the bulk of that code should work (Not sure if “len()” is right though, I never remember)

Edit: made some tweaks so we *shouldn’t* be overwriting any keywords.

sorry very confuse now

Im in England, is 18:35 and I think I will sleep on it and come back tomorrow

ill see if I can catch up with you tomorrow with a bit more of brain fresh

1 Like

Hello,

well, I’ve got some progress.

Now the program is asking to enter numbers until you type ‘-1’.

once typed ‘-1’ the while loop breaks and print "the average is: "

none of the average functions is working for me.

i tried mean(), (sum(x) / len(x)), and some others but I cant make it work.

any hints about the average calculation?

```
numbers = str(-1)
print("enter a numbers different then: ", numbers)
request = input("or -1 to calculate the average: ")
# average = numbers / numbers
while (numbers != request):
request = input("enter another number: ")
average = int(request)
average = request/len(request)
print("the average is: ", average)
```

Maybe this?:

```
sum = 0
count = 0
# Ask the user to enter a number
num = float(input("Enter a number (-1 to stop): "))
# Keep looping until the user enters -1
while num != -1:
# Add the number to the sum and increment the count
sum += num
count += 1
# Ask the user to enter another number
num = float(input("Enter a number (-1 to stop): "))
# Calculate the average (if there were numbers entered)
if count > 0:
average = sum / count
print("The average of the", count, "numbers entered is", average)
else:
print("No numbers were entered.")
```

2 Likes

the following basically gets as many valid numbers as possible until they input ‘stop’ (allows the person to input negative numbers). Code is super ugly and I normally wouldn’t write something like this but I wrote this completely here so

```
def one_num():
x = input("please input a number: ")
while not (x.isdigit() or (x[0] == '-' and x[1:].isdigit() or x == 'stop'):
x = input("please input a number: ")
if (x != 'stop'):
return int(x)
else:
return None
amt = 0
current = 0
while True:
x = one_num()
if (x is None): break
amt += 1
current += x
if (amt > 0):
print(current / amt)
else:
print("You need to input things -_-")
```

1 Like

I always think I know Python so well until I see a code that you make lol.

1 Like

I know, that happened to me when I was learning Japanese

you see, languages are all the same.

morphology, semantic, and syntaxis

2 Likes

Joining the frail with another silly version …

```
sum = 0
nums = 0
while True:
try:
new_number = int(input("type new number or -1 to stop: "))
except:
print("invalid number")
continue
if new_number == -1:
break
sum += new_number
nums += 1
if nums == 0:
print("no number given.")
else:
print("average is", sum/nums)
```

1 Like

oh nah, there’s no way! You are a much better programmer than me

lol yeah was gonna use try/catch but slow XD

you need to use the try/catch … something of an habit i hope gets picked up … otherwise move to go and manage the errors (which is much much better)

1 Like