# let's randomly pick a number between 0 and 9, and print its value if it is greater than 5

from random import randrange
r = randrange(10)

if r > 5: print(r) # when the condition (r > 5) is valid/true, the code (print(r)) will be executed
# you may need to execute your code more than once to observe an outcome

# repeat the same task four times, and also print the value of iteration variable (i) 

for i in range(4):
    r = randrange(10) # this code belongs to for-loop, and so it is indented
    if r > 5: # this code also belongs to for-loop, and so it is indented as well
        print("i=",i,"r=",r) # this code belongs to if-statement, and so it is indented with respect to if-statement

# if you are unlucky (with probabability less than 13/100), you may still do not see any outcome in a single run

# do the same task 100 times, and find the percentage of successful iteration (attempt)
# an iteration (attempt) is successful if the randomly picked number is greater than 5
# the expected percentage is 40, because, out of 10 numbers, there are 4 numbers greater than 5 
#     but the experimental results differ

success = 0
for i in range(100):
    r = randrange(10)
    if r > 5: 
        success = success + 1
print(success,"%")

# each run most probabily will give different percentage value

# let's randomly pick a number between 0 and 9, and says whether it less than 6 or not
# we use two conditionals here

r = randrange(10)
print("the picked number is ",r)

if r < 6:
    print("it is less than 6")
if r >= 6:
    print("it is greater than or equal to 6")

# let's write the same code by using if-else

r = randrange(10)
print("the picked number is ",r)

if r < 6:
    print("it is less than 6")
else: # if the above condition (r<6) is False
    print("it is greater than or equal to 6")

#
# your solution is here
#


#
# when there are many conditionals, we can use if-elif-else
#

# let's randomly pick an even number between 1 and 99
# then determine whether it is less than 25, between 25 and 50, between 51 and 75, or greater than 75.

r = randrange(2,100,2) # randonmly pick a number in range {2,4,6,...,98}, which satisfies our condition
# let's print this range to verify our claim
print(list(range(2,100,2)))

print() # print an empty line
print("the picked number is",r)

if r < 25:
    print("it is less than 25")
elif r<=50: # if the above condition is False and my condition is True
    print("it is between 25 and 50")
elif r<=75: # if both conditions above are False and my condition is True
    print("it is between 51 and 75")
else: # if all conditions above are False
    print("it is greater than 75")

#
# your solution is here
#


# let's determine whether a randomly picked number between -10 and 100 is prime or not.
# this is a good example for using more than one conditionals in different parts of the program
# this is also an example for "break" command, which terminates any loop immediately

r = randrange(-10,101) # pick a number between -10 and 100
print(r) # print is value

if r < 2: print("it is NOT a prime number") # this is by definition
elif r == 2: print("it is a PRIME number") # we already know this
else:
    prime=True # we assume that r is prime, and try to falsify this assumption by looking for a divisor in the following loop
    
    for i in range(2,r): # check all integers between 2 and r-1
        if r % i ==0: # if i divides r without any reminder (or reminder is zero), then r is not be a prime number
            print("it is NOT a prime number")
            prime=False # our assumption is falsifed
            break # TERMINATE the iteration immediately
        # we are out of if-scope
    # we are out of for-loop-scope
    if prime == True: # if our assumption is still True (if it was not falsied inside for-loop)
        print("it is a PRIME number")

# this is an example to write a function
# our function will return a Boolean value True or False

def prime(number): # our function has one parameter
    if number < 2: return False # once return command is executed, we exit the function
    if number == 2: return True # because of return command, we can use "if" instead of "elif"
    if number % 2 == 0: return False # any even number greater than 2 is not prime, because it is divisible by 2
    for i in range(3,number,2): # we can skip even integers
        if number % i == 0: return False # once we find a divisor of the number, we return False and exit the function
    return True # the number has passed all checks until now
    # because of return command, the program can be shorten
    #     remark that this might not be a good choice everytime for readibility of codes 

# let's test our program by printing all prime numbers between -10 and 30
for i in range(-10,30):
    # we pass i to the function prime
    if prime(i): # the function prime(i) returns True or False
        print(i) # this code will be executed if i is prime, i.e., prime(i) returns True