"""
Example: Write a code that, for a given positive number n, prints the sequence of numbers from 0 to n-1, separated by a comma
"""

n = int(input("Enter a positive number..."))

# Initialize the helper variable i
i = 0

# Loop and print the values from i to n-1
while i < n:

    # Below, end = is used to specify the characters to print last
    print(i, end = ", ")

    # Helper variable must be updated, to avoid an infinite loop
    i += 1

# Add an empty line in the output (terminal)
print()

"""
Examples of calling range() function
"""

# To print the sequence returned by range()
# we first convert it to a built in Python sequence,
# i.e., a list
# About lists: https://docs.python.org/3/glossary.html#term-list


# range(stop)
print("range(1) ==> ", list(range(1)))
print("range(5) ==> ", list(range(5)))
print("range(-5) ==> ", list(range(-5)))

# range(start, stop)
print("range(2, 5) ==> ", list(range(2, 5)))
print("range(-2, 5) ==> ", list(range(-2, 5)))
print("range(-2, -3) ==> ", list(range(-2, -3)))
print("range(-2, -1) ==> ", list(range(-2, -1)))
print("reversed(range(-1, 1)) ==> ", list(reversed(range(-1, 1))))

# range(start, stop, step)
print("range(2, 5, 2) ==> ", list(range(2, 5, 2)))
print("range(-2, 5, 3) ==> ", list(range(-2, 5, 3)))
print("range(-2, -8, -2) ==> ", list(range(-2, -8, -2)))
print("range(-2, -8, 2) ==> ", list(range(-2, -8, 2)))
print("reversed(range(1, 10, 2)) ==>", list(reversed(range(1, 10, 2))))

"""
Write a code that repeatedly asks for an integer number. If the received number n is negative, the program asks again. Otherwise, it computes n! and prints the result.
"""

ask_again = True
while ask_again:
    n = int(input("Enter a number..."))
    if n >= 0:
        n_factorial = 1
        for i in range(2, n + 1):
            n_factorial *= i
        print(f"{n}! = {n_factorial}")
        ask_again = False

"""
Example: Write a piece of code that takes an integer number and returns its number of digits. If the input is zero, the program outputs "Error: Entered number is zero. Exiting…"
"""

# Version 1: With loops
n = int(input("Enter a number…"))

if n == 0:
    print("Error: Entered number is zero. Exiting...")
else:
    v = -n if n < 0 else n
    digits = 0
    while v:
        digits += 1
        v //= 10
    print(f"There are {digits} digits in {n}.")

# Version 2: Without loops
n_str = input("Enter a number…")
n = int(n_str)
if n:
    digits = len(n_str) if n > 0 else len(n_str) - 1
    print(f"There are {digits} digits in {n}.")
else:
    print("Error: Entered number is zero. Exiting...")





