Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arithematic average
#1
I try it:

I create a variable average whose value is the arithmetic mean of num_a and num_b.

What is wrong my code? Is not work.


def find_average(num_a: int, num_b: int) -> float:
    numbers = [num_a, num_b]
    result = statsitics.mean(numbers)
    return = result
print(find_average(8, 4))
Thank you!
Reply
#2
You're close; there's just an error or two in your code, is all: check the spelling, for example.

def find_average(numbers):
    result = statistics.fmean(numbers)
    return result

print(find_average([8, 4]))
Have a look at the docs to see why I've used .fmean()
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
#3
plaese read about annotations here
Especially:
Quote:While these annotations are available at runtime through the usual __annotations__ attribute, no type checking happens at runtime. Instead, the proposal assumes the existence of a separate off-line type checker which users can run over their source code voluntarily. Essentially, such a type checker acts as a very powerful linter. (While it would of course be possible for individual users to employ a similar checker at run time for Design By Contract enforcement or JIT optimization, those tools are not yet as mature.)
you must explicity cast both inputs to integer, and output to float.
Reply
#4
Now im fine.

doing this.

def find_average(num_a: int, num_b: int) -> float:
    average = (num_a + num_b) // 2
    return average

print(find_average(18, 8))
Thank you all!
Reply
#5
(Nov-17-2023, 01:20 PM)deferender Wrote: Now im fine.

doing this.

def find_average(num_a: int, num_b: int) -> float:
    average = (num_a + num_b) // 2
    return average

print(find_average(18, 8))
Thank you all!

That's fine, but it's not at all flexible. What if you have more than two numbers? The main points about coding a function, is that said function should do as its name suggest and be useful for more than one particular circumstance.

In fact, what you have there does not even work correctly.
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
#6
Your first two posts and you run bang into one of the thornier controversies in python.

Numeric types and type annotations don't work well together in Python. It is dumb for your function that returns a float to limit itself to int inputs. Why can't I compute the average of 3.8 and 4.2? You might modify your function to be more generic.
def find_average(a: int | float, b: int | float) -> float:
    return (a + b) / 2
Later on you start using numpy and you find yourself wanting to use find_average() with numpy numbers which are different than int or float. You add support for numpy numeric types, but now your old programs that used find_average() stop running in python environments that don't have numpy installed.

I find myself torn about using type annotations. I have a predisposition to disliking type annotations because a big part of the allure of Python is that it isn't anything like strongly typed languages. If I wanted strong typing I would write in C++, not Python. However, I must admit that unit testing is not an adequate tool for finding all errors in python code. Unit testing depends on the test author predicting all possible failures, and I am not up to the task. Many times mypy has found errors that I missed in my testing, and mypy works better when it has more information. I also like how much better code completion works in my editor when type information is available.
Larz60+ likes this post
Reply
#7
(Nov-17-2023, 05:10 PM)deanhystad Wrote: Your first two posts and you run bang into one of the thornier controversies in python.

Numeric types and type annotations don't work well together in Python. It is dumb for your function that returns a float to limit itself to int inputs. Why can't I compute the average of 3.8 and 4.2? You might modify your function to be more generic.
def find_average(a: int | float, b: int | float) -> float:
    return (a + b) / 2
Later on you start using numpy and you find yourself wanting to use find_average() with numpy numbers which are different than int or float. You add support for numpy numeric types, but now your old programs that used find_average() stop running in python environments that don't have numpy installed.

I find myself torn about using type annotations. I have a predisposition to disliking type annotations because a big part of the allure of Python is that it isn't anything like strongly typed languages. If I wanted strong typing I would write in C++, not Python. However, I must admit that unit testing is not an adequate tool for finding all errors in python code. Unit testing depends on the test author predicting all possible failures, and I am not up to the task. Many times mypy has found errors that I missed in my testing, and mypy works better when it has more information. I also like how much better code completion works in my editor when type information is available.

Thank you! :)
Reply
#8
Just for fun, I was looking at finding numbers in a string.

If you collect numbers via input() you also get a string.

To get the average of a list of numbers-as-a-string:

import re
import statistics

# I just put some numbers in a sentence
# if you collect numbers from input() they will also be strings
mystring = '11当地时间11月16日上午,23国家主席习近平向在旧金山-55.543举行的亚太经合组织工54.7395商领导人峰会发表题为28.2698《同心协力 共迎挑战 44.3谱写亚太合-2.2356作新篇章》的书面演讲。'

def ameany(astring):
    # a pattern to find most, maybe not all, numbers in a string
    pattern = r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?"
    nums = re.findall(pattern, astring)
    numsf = [float(n) for n in nums]
    avg = statistics.fmean(numsf)
    return avg

average = ameany(astring)
print('The average is', average)
I think it was Larz60+ who told me about gmpy. To express floating point numbers to many many decimal places, thousands if you like, look at the module gmpy:

Read the docs for gmpy2:

import gmpy
Reply


Forum Jump:

User Panel Messages

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