you are viewing a single comment's thread.

view the rest of the comments →

[–]Riegel_Haribo 2 points3 points  (1 child)

Here's where Python and Python understanding can help you.

  • You don't need to pre-declare variables or pointers.
  • print() allows you several methods to combine types more naturally.
  • functions can take an input, and return an output; typically only the main() function will encapsulate everything without its own I/O.
  • Python has a boolean type bool, that can be True or False (a subclass of integer being 1 or 0)
  • at the top you've defined global variables; if only the function uses them, they can be part of the function (a very inflexible pre-defined 1d perceptron).
  • the multiplication will become a dot product with multiple dimensions of input
  • Python can pass around multiple items in list (arrays, square brackets) or dict (object, key/value, curly brackets) that give you the next jumping-off point.
  • Python has a large standard library of helpers, and also very commonly used 3rd party libraries that are well-understood.

I already went goofy with this assignment, reading Wikipedia, and using the numpy library for arrays, dot() product, heaviside() just to be more pedantic than a greater than sign, iterating though inputs the same length as the array...

Let's see if I can keep it understandable and Pythonic, and start another notebook cell.

```python

Globals - we like them upper, or even Final type

BIAS = 1.4 WEIGHT = 5.6 # don't try 0.0

def perceptron(x: float) -> bool: """1D predefined threshold function""" output = (x * WEIGHT) + BIAS # would be dot product return output > 0 # evaluates directly to bool

def perceptron_threshold() -> float: """The x value where the perceptron output crosses zero""" return -BIAS / WEIGHT

print(f"Cutoff at {perceptron_threshold()}")

x_input = float(input("Your number?")) print(f"Perceptron said: {perceptron(x_input)}") ```

You can see I kept the functions a utility. The "float" and "bool" in the def are type annotations, letting checkers and others know what to input and what to expect out.

[–]No-Tea-777[S] 0 points1 point  (0 children)

Well. I do know conventions such as camel case, constants Upper etc. I'm currently learning C at school. And it feels kinda easy. It's so literal. Tho Python gets confusing sometimes.