Prime numbers Python code

Saidieni mimi kuformat hii

def prime_number(num):
for x in range(2, num):
if (num % x == 0):
return False
else:
return True

def prime_number(num): for x in range(2, num): if (num % x == 0): return False else: return True

3 Likes

I can’t see the difference.

The difference is readability

1 Like

Ule mtoto wako mwenye alipita KCSE genes zake zilitoka kwa babake na c wewe

1 Like

kuwa mpole braza

Won’t the code run if it’s not reader-friendly? Code is meant to run, not to be read.

I don’t engage chokoras. Sorry.

In python indentation is part of it’s syntax, so the above non reader friendly version is not syntactically correct, see https://www.python.org/

2 Likes

indentation in python means alot.

1 Like

WhenI try to run it I get this error

"
test_prime_number_one

Failure in line 19, in test_prime_number_one self.assertEqual(result, False, msg=“Result is invalid”) AssertionError: Result is invalid"

beat me to it.

Ok. I wouldn’t try writing code in python if that’s the case then.

Code is supposed to be readable for maintenance

3 Likes

Your test case is failing, try some of the methods mentioned here

1 Like

It’s not just python, correct indentation is very important, unless it’s a contest for writing most obfuscated code

1 Like

Mimi shida ni comments…
I rarely write comments kinda assume nimeiva will remember

That’s completely unacceptable in my rule book, I can condone slow developers but I only give one warning when it comes to readability, comments and general good coding practice

Na mnipe ya hii ya python

Create both a recursive function called recursive_factorial and iterative function called iterative_factorial that does the following

[ul]
[li]Accepts as parameter an Integer n[/li][li]Computes the factorial of n[/li][li]Returns the factorial of n[/li][/ul]

That’s until you look at the code a few months down the line and you can’t remember whatever it is you were doing… you end up wasting time hitting the books/net just so that you can figure YOURSELF out.

1 Like