Reputation: 192
I would like the format of this data to me uniform for the user to read but I cannot with everything I have tried. Code here :
def runModel():
global valueJuvenile, valueAdult, valueSenile,total, values
values = ''
total = valueSenile + valueJuvenile + valueAdult
values += 'G' + ', '
values += 'Juv' + ', '
values += 'Adu' + ', '
values += 'Sen' + ', '
values += 'Tot' + ', '
values += '\n0' + ', '
values += str(valueJuvenile) + ', '
values += str(valueAdult) + ', '
values += str(valueSenile) + ', '
values += str(firstTotal) + ', '
for n in range(1,numNewGen):
if n != 0:
values += '\n'
values += str(n)+', '
valueJuvenile = round(valueAdult * birthRate * valueJuvenileSR,3)
valueAdult = round(valueJuvenile * valueAdultSR,3)
valueSenile = round(valueSenile + valueAdult * valueSenileSR,3)
total = round(valueSenile + valueJuvenile + valueAdult,3)
values += str(valueSenile) + ', '
values += str(valueJuvenile) + ', '
values += str(valueJuvenile) + ', '
values += str(total)
print(values)
print("Model has been ran!")
input('\nPlease press Enter to return to menu...')
menu()
and here is the outcome in the shell:
values set:
Please enter the amount of Juveniles (1 = 1000) 1
Please enter the amount of Adults (1 = 1000) 1
Please enter the amount of Seniles (1 = 1000) 1
Please enter the survival rate for Juveniles 1
Please enter the survival rate for Adults 1
Please enter the survival rate for Seniles 1
Please enter the birth rate of GreenFlies 1
Please enter the number of new generations 11
Please enter at what population breakpoint would you like the disease to trigger 11
Running model!
G, Juv, Adu, Sen, Tot,
0, 1.0, 1.0, 1.0, 3.0,
1, 2.0, 1.0, 1.0, 4.0
2, 3.0, 1.0, 1.0, 5.0
3, 4.0, 1.0, 1.0, 6.0
4, 5.0, 1.0, 1.0, 7.0
5, 6.0, 1.0, 1.0, 8.0
6, 7.0, 1.0, 1.0, 9.0
7, 8.0, 1.0, 1.0, 10.0
8, 9.0, 1.0, 1.0, 11.0
9, 10.0, 1.0, 1.0, 12.0
10, 11.0, 1.0, 1.0, 13.0
Model has been ran!
as you can see at row 0 the data isn't in the same sort of position as the others, this also happens at row 10 because the 0 is a extra character it shifts everything one place to the right and I'd like to know how to format this.
For context, this is meant to be a program where the user sets variables and those variables are adjusted by other variables set for example value of juveniles being affected by the survival rate of juveniles for the next generation. In the output shell the G corresponds to the number of generations in this case 0 being the original values set by user through to generation 10 which has already been affect by the other variables 10 times by that point. The Juv, Adu and Sen correspond to juvenile adult and senile
Upvotes: 1
Views: 605
Reputation: 138
You can use C style format strings, or the .format method.
As an example using C style format strings, you can do:
print("%2i, %5.1f, %5.1f, %5.1f" % (3, 11.2, 5.0, 77.5))
print("%2i, %5.1f, %5.1f, %5.1f" % (11, 12.7, 11.4, 8.1))
This would give the following results:
3, 11.2, 5.0, 77.5
11, 12.7, 11.4, 8.1
The basic rule is that %5.1f makes a floating point number take 5 characters, including one decimal digit. The padding characters will be spaces. So if you wanted to take 8 characters and have 3 decimal digits for a float, you could do %8.3f. Or if you wanted 4 characters and no decimal points for a float, you could do %4.0f.
Similarly, for an integer %2i makes an integer take up 2 characters width. If you wanted it to take 6 characters for an integer, you could do %6i. If you wanted zero padding, you could add a zero to the format string like this: %06i. Note that %i and %d are equivalent.
Upvotes: 1
Reputation: 2069
The format
function will do the trick.
Here an example on how to use it:
>>> integer_var = 5
>>> float_var = 3.5
>>> print('{:6d} {:6.2f}'.format(integer_var, float_var))
5 3.50
For integer variables use d
for float variables f
. The first number after the :
is the space the number should take. In the case of floating point numbers, the number after the .
is the number of decimal points numbers to show
Upvotes: 2
Reputation: 42748
Use format
. For example:
def runModel(valueJuvenile, valueAdult, valueSenile,total, values):
header = ['G', 'Juv', 'Adu', 'Sen', 'Tot']
format = "{:5}, {:5}, {:5}, {:5}, {:5}".format
values = [format(*header)]
values.append(format(n, valueJuvenile, valueAdult, valueSenile, firstTotal))
for n in range(1, newNewGen):
valueJuvenile = round(valueAdult * birthRate * valueJuvenileSR,3)
valueAdult = round(valueJuvenile * valueAdultSR,3)
valueSenile = round(valueSenile + valueAdult * valueSenileSR,3)
total = round(valueSenile + valueJuvenile + valueAdult,3)
values.append(format(n, valueJuvenile, valueAdult, valueSenile, total))
print('\n'.join(values))
print("Model has been ran!")
input('\nPlease press Enter to return to menu...')
menu()
Upvotes: 2