0

I have the following exercise:

Annotate with correct types the parameters and the return values of the functions, as well as of the variables, in the program in the other panel. For that, you only need to replace every occurrence of Any, except for the very first line, by an appropriate type.

Example: The first Any in line 3, i.e., n: Any, must be replaced by int. You can see that from the line 9.

While I understand the theory of type hints I don't get the exercise, most probably because I do not get at this point the use of Any. Furthermore the exercise is ambiguous, it says to replace every occurrence of Any.

If this is the case would the first line be:

def get_half(n : int) -> int:
  return n/2

If so it does not work.

from typing import Any #do not edit this line

def get_half(n : Any) -> Any:
  return n/2

def print_half(n : Any) -> Any:
  print(n/2)
  
y = get_half(10)
print_half(20)

def is_present(s : Any, char : Any) -> Any:
  found : Any = False
  l : Any = len(s)
  for i in range(0, l):
    if s[i] == char:
      found = True
  return found

print(is_present("john", "h"))

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Emilia Delizia
  • 333
  • 3
  • 14
  • 1
    What exactly is puzzling you? [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) means any type, and the exercise unambiguous - you should replace them _all_ with more specific types. The one example you've given is indeed wrong, because `n/2` where `n: int` isn't going to give you an `int`. – jonrsharpe Nov 02 '21 at 09:51
  • the exmaple says Example: The first Any in line 3, i.e., n: Any, must be replaced by int. You can see that from the line 9. – Emilia Delizia Nov 02 '21 at 09:52
  • 1
    Yes, _"the first Any"_. `def get_half(n: int) -> Any: return n / 2` would be fine as a start, you can try it out in e.g. https://mypy-play.net/?mypy=latest&python=3.10. – jonrsharpe Nov 02 '21 at 09:55
  • Dividing an `int` does not always give an `int` in return, sometimes it could be `float`. Another example would be `found : Any = False` - seeing as the value is set as `False` means the datatype of `found` should be `bool`. – Tõnis Piip Nov 02 '21 at 09:55
  • Can you give me the first solution? def get_half(n : int) -> Any: return n/2 – Emilia Delizia Nov 02 '21 at 09:58
  • I think `-> Any` should be `-> float`. I hope you understand why: https://docs.python.org/3/tutorial/floatingpoint.html – Tõnis Piip Nov 02 '21 at 10:01
  • The error from using the wrong type already tells you what the right type should be: `Incompatible return value type (got "float", expected "int")`. Alternatively you can actually try running the code with some input values and using `type` to find out what type(s) it _does_ return. – jonrsharpe Nov 02 '21 at 10:02
  • either solutions I get an error def get_half(n : Any) -> float: return n/2 Mypy output: type_hints_simple.py:3: error: Explicit "Any" is not allowed def get_half(n : Any) -> float: ^ – Emilia Delizia Nov 02 '21 at 10:06
  • @EmiliaDelizia You still have to change the first `Any`, not just the other. You have to change all `Any`s to the correct Python datatype such as `int`, `float` and `str` for example. There are more datatypes than these three, obviously, but nevertheless have a look at this: https://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5 – Tõnis Piip Nov 02 '21 at 10:13
  • You're supposed to replace _all_ of the `Any`s, not just one at a time: _"replace every occurrence of Any, except for the very first line"_. – jonrsharpe Nov 02 '21 at 10:15

2 Answers2

1

This is the correct solution thanks to your answers

This link was very useful

https://mypy-play.net/?mypy=latest&python=3.10

from typing import Any #do not edit this line

def get_half(n : int) -> float:
  return n/2

def print_half(n : int) -> None:
  print(n/2)
  
y = get_half(10)
print_half(20)

def is_present(s : str, char : str) -> bool:
  found : bool = False
  l : int = len(s)
  for i in range(0, l):
    if s[i] == char:
      found = True
  return found

print(is_present("john", "h"))

Emilia Delizia
  • 333
  • 3
  • 14
0

The first any is an int like the example said def get_half(n : int) -> Any: and the return value will differ depending if your exercise is for python 2 or 3.

Python2: def get_half(n : int) -> int:

Python3: def get_half(n : int) -> float:

The reason for this is that Python 2 always returns an int if you divide and int with an int. int/int=int And Python 3 uses "true division" and therefor returns an float. int/int=float

For more info see: https://en.wikibooks.org/wiki/Python_Programming/Operators

Sefan
  • 699
  • 1
  • 8
  • 23
  • 1
    Although you're right about integer division in Python 2, versions prior to 3.0 _didn't have function annotation syntax_ (see [PEP 3107](https://www.python.org/dev/peps/pep-3107/)) and the semantics weren't defined before 3.5 (see [PEP 484](https://www.python.org/dev/peps/pep-0484/)). – jonrsharpe Nov 02 '21 at 10:16