Practice Log: June 11, 2025
# Exercise: Sum of Digits
# Goal: Write a Python function that takes a non-negative integer as input and returns the sum of its digits.
# Example:
# If the input is 123, the output should be 1 + 2 + 3 = 6.
# If the input is 405, the output should be 4 + 0 + 5 = 9.
# If the input is 7, the output should be 7.
# This exercise is great for practicing loops (or recursion, if you're feeling adventurous!) and basic arithmetic operations like modulo (%) and integer division (//) in Python
num = input("Give me a non-negative number: ")
def digitsSum(num):
result = 0
for i in range(0, len(num)):
result += int(num[i])
return result # print(result) //better return the value in functions
calculate_sum = digitsSum(num)
print(f"The sum of nums is {calculate_sum}")
# Exercise: Prime Number Checker
# Goal: Write a Python function that takes a positive integer as input and determines if it is a prime number.
# Definition:
# A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
# Examples: 2, 3, 5, 7, 11 are prime numbers.
# Examples: 4 (divisible by 2), 6 (divisible by 2 and 3), 1 are not prime numbers. (Note: 1 is explicitly not a prime number).
# Task:
# Create a function, say is_prime(n), that returns True if n is prime, and False otherwise.
#my solution
num = int(input("Give me a non integer nunmber"))
def isPrime(num):
isPrime = True
for i in range(2, num):
if num % i == 0:
isPrime = False
return isPrime
result = isPrime(num)
if result:
print("Prime")
else:
print("No Prime")
# Gemini Feedback
# Suggestion: Clarify the input prompt
# num_input = int(input("Give me a positive integer: "))
num_input_str = input("Give me a positive integer: ") # Let's get it as string first for better error handling
def is_prime_checker(number): # Renamed function parameter for clarity
# 1. Handle edge cases first: Prime numbers are greater than 1.
if number < 2:
return False # Numbers less than 2 are not prime (0, 1, negative numbers)
# 2. Check for divisibility
# 'prime_status' is a clearer variable name than reusing the function name 'isPrime'
prime_status = True
# Loop from 2 up to number - 1
for i in range(2, number):
if number % i == 0:
prime_status = False
break # Optimization: If we find a factor, we know it's not prime, so we can stop checking.
return prime_status
# It's good practice to validate input, especially when converting types
try:
num = int(num_input_str)
# Now call the function and print the result
if is_prime_checker(num):
print(f"{num} is Prime")
else:
print(f"{num} is not Prime") # "No Prime" is okay, but "is not Prime" is more descriptive
except ValueError:
print("Invalid input. Please enter a whole number.")