0

I've been working on this program for around 4 days and it's due tonight so I really need some help. I've tried numerous ways to get it to print but I need other functions but the ones I try don't work. One of the functions I need to make is supposed to receive the minimum, maximum, and step size as arguments, and print out the table. Making that function would really help. There's supposed to be another function made as well that rounds the log of the functions on the left side of the table to four decimal places.

Here's a link to what the program is supposed to print: enter image description here

This what my code prints:

What's your minimum value?1.0
What's your maximun value?2.5
What is your step size?0.1
1.0 0.0
1.1 0.04139
1.2000000000000002 0.07918
1.3000000000000003 0.1139
1.4000000000000004 0.1461
1.5000000000000004 0.1761
1.6000000000000005 0.2041
1.7000000000000006 0.2304
1.8000000000000007 0.2553
1.9000000000000008 0.2788
2.000000000000001 0.301
2.100000000000001 0.3222
2.200000000000001 0.3424
2.300000000000001 0.3617
2.4000000000000012 0.3802
  0    1    2    3    4    5    6    7    8    9    
---------------------------------------------------------

Here's my code that semi prints out the 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, step):
while min_num < max_num:
    print(f"{min_num} {math.log10(min_num):.4}")
    min_num += step 

min_value = min_num()
max_value = max_num(min_value)
stepSize = get_step()
logtable(min_value, max_value, stepSize)

print("      0    1    2    3    4    5    6    7    8    9    ")
print("---------------------------------------------------------")
  • Is not clear how every column should be calculated. About how to format a float as a string, you can see this question https://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python – Gonzalo Odiard Feb 16 '22 at 01:47
  • Ok, do you know how to make a function that takes a number as an argument and returns the log of that rounded to 4 decimal places? – Demarcus Thompson Feb 16 '22 at 01:52

2 Answers2

1

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

"""
JennyA
  • 46
  • 5
  • Instead of using 1.5, could you use another function or does it have to be a number? Also how do I call the function you gave – Demarcus Thompson Feb 16 '22 at 02:05
  • This function accepts an integer or a float only, you can't use a string. And to call the function, you just have to do calc_log(the_number). – JennyA Feb 16 '22 at 02:11
  • Also, if you want to assign it to a variable, all you have to do is: x = calc_log(the_number). But, how do you calculate the rest of the numbers? It's better if you would make that clear. For the first column, you calculate it using math.log10(num). What about the rest of the columns? – JennyA Feb 16 '22 at 02:12
  • So whenever I run the code it says num is not defined – Demarcus Thompson Feb 16 '22 at 02:14
  • Omg I'm soooooooo sorry, so in the picture, you enter a min and max value and it counts by 1 until you reach the max value. The rest of the columns that count through 0-10 are supposed to calculate log base1, log base 2, log base 3 and so on of the min and max values. – Demarcus Thompson Feb 16 '22 at 02:18
  • You need to define value of num before running the code. num = 1.5 then result = calc_log(num) then you can print(result). – JennyA Feb 16 '22 at 02:21
  • math.log(1.5, 2) = 0.5849625007211562 math.log(1.5, 3) = 0.3690702464285425...these are not the values in the table. Are you sure this is the right calculation? log of 1.5 to base 2 is 5849625007211562 and log of 1.5 to base 3 is 3690702464285425... – JennyA Feb 16 '22 at 02:27
  • Ummm let me see – Demarcus Thompson Feb 16 '22 at 02:34
  • The next 10 columns will show the logarithms of the numbers in the first column with an ever changing least significant digit. For example, if the number in the first column is 1.4, then the 10 columns after that will have the logarithmic values for 1.40, 1.41, 1.42, 1.43, 1.44, ..., 1.49 (which turn out to be 0.1461, 0.1492, 0.1523, 0.1553, 0.1584, ..., 0.1732 rounded to 4 decialm places – Demarcus Thompson Feb 16 '22 at 02:41
  • Hey, I have edited my answer. You can get the desired output with the new function. – JennyA Feb 16 '22 at 02:59
  • You can use your own min_num, max_num and get_step functions to accept and validate the input. And when you run the log_table function, use stepSize instead of steps. It will raise an error saying steps is not defined if you are using the get_step method to accept the number of steps. So you have to do: print(log_table(min_num, max_num, stepSize)) – JennyA Feb 16 '22 at 03:06
  • Hey, so some of that stuff we haven't learned and I'm not sure what it means. So would there be any way you could make the code more understandable like mines? Im sorryyyyy – Demarcus Thompson Feb 16 '22 at 03:08
  • Ok I'll try that and see if it works – Demarcus Thompson Feb 16 '22 at 03:08
  • Would you be willing to try to make the code again, more understandable like mine? It's fine if you cant, I'll try to figure something else out. I could edit the post and provide the template he gave us? – Demarcus Thompson Feb 16 '22 at 03:17
  • Here you go! I already edited it for you cause I thought it might be confusing. – JennyA Feb 16 '22 at 03:18
  • Omg you're a lifesaver. Truly thank you so much – Demarcus Thompson Feb 16 '22 at 03:19
  • Let me know if you are finding it difficult to understand the code. I will add comments on the code to explain it. – JennyA Feb 16 '22 at 03:20
  • Yea could you do that for the one logtable function cause I'm not sure what the join things do? – Demarcus Thompson Feb 16 '22 at 03:21
  • Oh okay I will...within about 30 minutes I hope... – JennyA Feb 16 '22 at 03:23
  • Ok thanks again so much – Demarcus Thompson Feb 16 '22 at 03:24
  • No problem, let me know if it's not clear. I have edited it for you. – JennyA Feb 16 '22 at 03:52
  • What does the last print function do? – Demarcus Thompson Feb 16 '22 at 04:17
  • print(" "*2 + min_num_formatted + " "*8 + " ".join(l))...This one first prints 2 spaces, then the min_num_formatted (the one that has only 4 decimal points after it) then it prints the list of log results, for the first round where min_num is 1.50 the list will be ['0.1761', '0.1790', '0.1818', '0.1847', '0.1875', '0.1903', '0.1931', '0.1959', '0.1987', '0.2014'] then on the second round where min_num is 1.61 list l will be ['0.2041', '0.2068', '0.2095', '0.2122', '0.2148', '0.2175', '0.2201', '0.2227', '0.2253', '0.2279'] and so on. So ' " ".join(l) ' prints the list separated by 2 spaces. – JennyA Feb 16 '22 at 04:44
  • Added more comments on the code. Hope it clarifies it more. – JennyA Feb 16 '22 at 05:09
  • Thank you again, I also had another question about another program but I'll wait until tomorrow. If you could help with that one – Demarcus Thompson Feb 16 '22 at 05:35
  • It is my pleasure! – JennyA Feb 16 '22 at 19:12
0

You can easily create tables using the 'tabulate' library.And the logtable part has been modified so that the log value is displayed properly. First install 'tabulate' in cmd with the code below.

pip install tabulate

And if you use the code below, you can output similarly, although not the same table as the image.

import math
from tabulate import tabulate

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, step):
    print_list = list()
    while min_num < max_num:
        content = list()
        content.append(round(min_num, 5))
        for i in range(10):
            content.append(round(math.log10(min_num + i/100), 4))
        print_list.append(content)
        min_num += step 
    print(tabulate(print_list, headers=[' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], floatfmt='.4f'))

if __name__ == "__main__":
    min_value = min_num()
    max_value = max_num(min_value)
    stepSize = get_step()
    logtable(min_value, max_value, stepSize)
Desty
  • 328
  • 1
  • 5
  • We haven't learned how to use tabulate. Is there possibly any other way to make the table??? – Demarcus Thompson Feb 16 '22 at 02:21
  • @DemarcusThompson The way to output a table using list is by using the tabulate library, and the way to implement the table itself is by using pandas. – Desty Feb 16 '22 at 02:39
  • @DemarcusThompson Additionally, I think there is one problem with this code. Since '100' in the 'min_num + i/100' part is arbitrarily designated, it is necessary to find the number of decimal places in the log base. – Desty Feb 16 '22 at 02:43
  • Is there no other way to print out the columns? – Demarcus Thompson Feb 16 '22 at 02:58
  • @DemarcusThompson It would be convenient to print columns using pandas, but in the current code, the values ​​are stored as a list, so you have to print them with a for loop. – Desty Feb 16 '22 at 07:41