33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
"""
|
|
Write a Python program that can take a positive integer greater than 2 as input and write out the number of times
|
|
one must repeatedly divide this number by 2 before getting a value less than 2.
|
|
"""
|
|
|
|
|
|
def count_steps():
|
|
# input validation: try that input is an integer
|
|
while True:
|
|
try:
|
|
current_value = int(input("Please enter an integer greater than 2: "))
|
|
# also try that input is above 2, if not print error message
|
|
if current_value <= 2:
|
|
print("Invalid input: Please enter only integers greater than 2.")
|
|
# if input is correct, break the while loop
|
|
else:
|
|
break
|
|
# Handle ValueError and print error message
|
|
except ValueError:
|
|
print("Invalid input: Please only enter integers.")
|
|
|
|
# set 'step_count' variable to count divisions
|
|
step_count = 0
|
|
# iterate divisions over 'current_value' until it is lower than 2
|
|
while current_value >= 2:
|
|
current_value = current_value / 2
|
|
# add division step to 'step_count'
|
|
step_count += 1
|
|
print(step_count)
|
|
|
|
|
|
count_steps()
|