This function accepts a number and returns the log of that rounded to 4 decimal places.
# import math
# def calc_log(num):
# return format(math.log10(num), '.4f')
# print(calc_log(1.5)) # 0.1761
# This is the function that accepts the inputs and prints the log table...
import math
def min_num():
while True:
i = float(input("What's your minimum value? "))
if i > 0:
return i
print("ERROR. Minimum should be greater than 0")
def max_num(min_num):
while True:
i = float(input("What's your maximun value? "))
if i > min_num:
return i
print(f"ERROR. Maximum value must be greater {min}")
def get_step():
while True:
step = float(input("What is your step size? "))
if step > 0:
return step
print("ERROR. Step size should be greater than 0")
def logtable(min_num, max_num, steps):
# prints a line
print()
"""
This one prints 2 things.
1. " "*16 --> this prints space 16 times (If we run print("a"*6), we will get "aaaaaa")
2. " ".join([str(i) for i in range(0, 10)])
- This one does the following:
a. It converts every number from 0 to 10 (10 is not included) to a string and adds it to a list.
- Let's take a list "l":
l = [str(i) for i in range(0, 10)]
- The above is is equivalent to:
l = [] # creating an empty list..
for i in range(0, 10):
l.append(str(i)) # append 0 to 10 (10 not included) after converting each to a string
b. It prints the list separated by 7 spaces.
- If we had a list ['a', '1', 'b']:
print(" ".join(['a', '2', 'b'])) --> a 2 b
print("-".join(['a', '2', 'b'])) --> a-2-b
print("---".join(['a', '2', 'b'])) --> a---2---b
"""
print(" "*16 + " ".join([str(i) for i in range(0, 10)]))
# prints "-" 94 times
print("-"*(94))
# while loop
while True:
# This if statement checks if the min_num is greater than or equal to the max_num and stops/breaks the while loop if the condition is satisfied
if min_num >= max_num:
break
# This just formats the min_num value to have only 4 decimal places
min_num_formatted = str(format(min_num, '.4f'))
# This creates an empty list
l = []
# This is a for loop that runs 10 times with i value from 0 to 10 (10 not included)
for i in range(10):
"""
It appends the formated value of log of the min_num plus the current value of i divided by 100
l.append(str(format(math.log10(min_num+(i/100)), '.4f')))
The first loop: i = 0
str(format(math.log10(min_num+(0/100)), '.4f') = str(format(math.log10(min_num+0.0), '.4f')) = str(format(math.log10(1.5+0.0), '.4f')) = str(format(math.log10(1.5), '.4f')) = str(format(0.17609125905568124, '.4f')) = str(0.1761) = "0.1761"
The second loop: i = 1
str(format(math.log10(min_num+(0/100)), '.4f')) = str(format(math.log10(min_num+0.1), '.4f')) = str(format(math.log10(1.5+0.1), '.4f')) = str(format(math.log10(1.51), '.4f')) = str(format(0.2041199826559248, '.4f')) = str(0.2041) = "0.2041"
and so on...
This one does not print anything. it just adds/appends the log values to the list.
"""
l.append(str(format(math.log10(min_num+(i/100)), '.4f')))
"""
So after the for loop runs 10 times, the value of l will be:
First step (min_value=1.50000): l = ['0.1761', '0.1790', '0.1818', '0.1847', '0.1875', '0.1903', '0.1931', '0.1959', '0.1987', '0.2014']
Second step (min_value=1.60000): l = ['0.2041', '0.2068', '0.2095', '0.2122', '0.2148', '0.2175', '0.2201', '0.2227', '0.2253', '0.2279']
Third step (min_value=1.70000): l = ['0.2304', '0.2330', '0.2355', '0.2380', '0.2405', '0.2430', '0.2455', '0.2480', '0.2504', '0.2529']
"""
# The new min_num will be changed by adding the steps to it
min_num += steps
# This one prints 2 spaces, then the formatted min_num value, and then the current list of log values each separated by " "
# The min_num_formatted is the value that was used before the steps were added to it
print(" "*2 + min_num_formatted + " "*8 + " ".join(l))
return
min_value = min_num()
max_value = max_num(min_value)
stepSize = get_step()
logtable(min_value, max_value, stepSize)
"""
This is the output:
What's your minimum value? 1.5
What's your maximun value? 3.5
What is your step size? 0.1
0 1 2 3 4 5 6 7 8 9
----------------------------------------------------------------------------------------------
1.5000 0.1761 0.1790 0.1818 0.1847 0.1875 0.1903 0.1931 0.1959 0.1987 0.2014
1.6000 0.2041 0.2068 0.2095 0.2122 0.2148 0.2175 0.2201 0.2227 0.2253 0.2279
1.7000 0.2304 0.2330 0.2355 0.2380 0.2405 0.2430 0.2455 0.2480 0.2504 0.2529
1.8000 0.2553 0.2577 0.2601 0.2625 0.2648 0.2672 0.2695 0.2718 0.2742 0.2765
1.9000 0.2788 0.2810 0.2833 0.2856 0.2878 0.2900 0.2923 0.2945 0.2967 0.2989
2.0000 0.3010 0.3032 0.3054 0.3075 0.3096 0.3118 0.3139 0.3160 0.3181 0.3201
2.1000 0.3222 0.3243 0.3263 0.3284 0.3304 0.3324 0.3345 0.3365 0.3385 0.3404
2.2000 0.3424 0.3444 0.3464 0.3483 0.3502 0.3522 0.3541 0.3560 0.3579 0.3598
2.3000 0.3617 0.3636 0.3655 0.3674 0.3692 0.3711 0.3729 0.3747 0.3766 0.3784
2.4000 0.3802 0.3820 0.3838 0.3856 0.3874 0.3892 0.3909 0.3927 0.3945 0.3962
2.5000 0.3979 0.3997 0.4014 0.4031 0.4048 0.4065 0.4082 0.4099 0.4116 0.4133
2.6000 0.4150 0.4166 0.4183 0.4200 0.4216 0.4232 0.4249 0.4265 0.4281 0.4298
2.7000 0.4314 0.4330 0.4346 0.4362 0.4378 0.4393 0.4409 0.4425 0.4440 0.4456
2.8000 0.4472 0.4487 0.4502 0.4518 0.4533 0.4548 0.4564 0.4579 0.4594 0.4609
2.9000 0.4624 0.4639 0.4654 0.4669 0.4683 0.4698 0.4713 0.4728 0.4742 0.4757
3.0000 0.4771 0.4786 0.4800 0.4814 0.4829 0.4843 0.4857 0.4871 0.4886 0.4900
3.1000 0.4914 0.4928 0.4942 0.4955 0.4969 0.4983 0.4997 0.5011 0.5024 0.5038
3.2000 0.5051 0.5065 0.5079 0.5092 0.5105 0.5119 0.5132 0.5145 0.5159 0.5172
3.3000 0.5185 0.5198 0.5211 0.5224 0.5237 0.5250 0.5263 0.5276 0.5289 0.5302
3.4000 0.5315 0.5328 0.5340 0.5353 0.5366 0.5378 0.5391 0.5403 0.5416 0.5428
"""