Home > Enterprise >  How to print out a logtable?
How to print out a logtable?

Time:02-16

I'm trying to figure out how to make a log table for a python code assignment. The code I have right now doesn't print out a column of it just prints out the log of those two numbers I enter.

Here's what I need the code to do: It needs to receive the min and max values and return the log of those. It also has to print out a column of 10 log functions. For example, if you enter 1.2 for your min and 1.9 for your max, it counts by one like 1.2, 1.3, 1.4, 1.5, and so on.

Here's the code:

 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)

CodePudding user response:

You are casting your input responses to integers, so if you input 1.2, it would be parsed as 1. If you are allowed to use numpy, then this can be simplified significantly:

import numpy as np
minv = 1.2  # you can grab these with input()
maxv = 1.91
step = 0.1

vals = np.arange(minv, maxv, step)
print(np.log10(vals))

Note that I used maxv=1.91 instead of maxv=1.9 to avoid cutoff at 1.9.

If you can't use numpy, then first you need to fix a few things in your code:

  • replace I with i in function return statements;
  • add a closing parenthesis in get_step() call;
  • replace int() with float() in your input() statements if you're dealing with floats instead of ints.
  • Related