Help to understand and write program to calculate average

yeah I always use try catch lol just in this case I knew a trick to catch any ints

I saw, you went checking if there is a non numeric, which is also nice and probably faster

1 Like

thanks yeah I always have this irky feeling when I use try catch too much because a fundamental I learned was that it’s slow lol

but then you made a function slowing everything down :smiley:

1 Like

well function calls in python are faster because of some weird python trick that has to do with the local vars speeding up the interpreter :stuck_out_tongue: lol idk XD

Exception suck. A reason why i used for many years go or languages managing errors and not using exceptions

1 Like

ohhh nice that’s a good idea :+1:

I get mixed up with the type of the variables.
i dont know when to change the type from str to int to calculate.

with my last modification I get the sum of the numbers but when I enter -1 the program just deduce 1 of the list.
hope people understand my explanation

1 Like

there are worse (lazier) ways like … appending all to a list and using mean … :slight_smile:

1 Like

oh yeah you could do that too XD

you have various options, but i would say check, convert and sum. (not what i did btw)

1 Like

which type are the caracters once are in a variable?
mean
x = 1 … is thi a ‘int’ or can it be a ‘str’?

1 Like

input gives you a string

1 Like
x = 1
input("what number: ")
print(x)

in this case x would be a ‘number’ and the input, whatever I write will always be a string
is that correct?
and the print will print a number, not a string?

morphology… semantic… the hell what tha’i’t

x=1, x is a number
x=input(), x is a string
print(x), converts to string and prints to console

Question. Does floats count as a valid input?

Yes. Floats count as valid input. But the float will be converted to a string. You can convert it back using float(x).

for some reason makes the addition of the numbers entered but when I enter ‘-1’ to calculate the average make a subtraction, (obviousli… -1) and doesn’t calculate the average
numbers: 5 5 5 = 15
break number: -1
average -14 :face_with_raised_eyebrow: :thinking: :scream: :face_in_clouds:

numbers = -1
count = 0

print("enter a numbers different then -1 ")
request = input("or -1 to calculate the average: ")
request = int(request)
count += request
# average = numbers / numbers
while (numbers != request):  
  request = input("enter another number: ")
  count = int(count)
  request = int(request)
  count += request

# request = int(request)
# count = int(count)
average = count/request
print("the average is: ", average)

because you have count += request before you check if it is -1.
The sequence should be: get number, check, add or calculate

1 Like