I need python help, i'm sure my question is pretty basic but i just cant figure it out.
Well, what is the question?
alright so i have to write a program for class from scratch i need to figure out how to print a line
it has to say
print("Shares Price Total")
but i need it spaced out, like to align with the numbers for each colum
What version of Python?
3.4.1
this is a picture of my code
this is a picture of my output
You can use similar tools to format the headder.
Oh, and you can inline post your code here if you put \(\text{```}\) abive and below it. Just as an example: ``` bla = input("Bla: ") x = 7 for y in range(x): print(y) ``` There are also code past sites, like pastebin: http://pastebin.com/
''' print(format(stockPurch,"10.0f"),format(stockCost,'10.2f'),format(total1,"10.2f")) '''
didnt work
Not single quotes. Accents. With the ~ on the key.
oooh ``` print(format(stockPurch,"10.0f"),format(stockCost,'10.2f'),format(total1,"10.2f")) ```
ah thats neat
thanks
np.
alright let me see if i can figure this out
``` stockPurch = 17043 stockCost = 7.9 total1 = stockPurch * stockCost stockPurch1 = 170 stockCost1 = 27.9 total2 = stockPurch1 * stockCost1 print(" Shares Price Total") print('Shares'.rjust(10)+'Price'.rjust(11)+'Total'.rjust(11)) print(format(stockPurch,"10.0f"),format(stockCost,'10.2f'),format(total1,"10.2f")) print(format(stockPurch1,"10.0f"),format(stockCost1,'10.2f'),format(total2,"10.2f")) ```
oh wow you did that so fast..
So you can manually justify it, or use rjust to do an automated one. The rjust method is good for something like format that changes depending on the size of the numbers involved.
Not really. I just knew what code I was looking for in that long list.
yeah i was trying the rjust but i thought i had to use rjust,ljust and center, one for each title, so i was wrong about that obviously.
Hehe. No, it is just for a string. Here is another thought, which is semi flawed: ``` print('{0:10}{1:10}{2:10}'.format('Shares', 'Price', 'Total')) ``` That shows you another use of format, but the passing isa after the string, not in front.
spacing... ignore the lysdexia.
oh yeah thats the one my teach used in an example but its not the way we were taught to do it (just didntget that far yet) but i couldnt remember how she did it
haha
lysdexia
Yep. makes coding interesting.
awesome that stuff worked and makes so much sense now, i just didnt know to only use one of the different rjust,ljust etc.
Yah, and you can see how the manual spacing worked.
yeah what made you choose those numbers in the ()?
the 10,11, and 11
I started withy 10, 10, 10. Now, I could have thought about your format with decimal (10 char + decimal) some more and used that logic to do it.
If there are fractional shares possible it would be 11, 11, 11.
ah i gotcha. so lets say my format for the number input was ``` format(x,"15.3") ``` would i do ``` print('x'.rjust(19)) ``` to make it line up correctly?
actually ill play with it and figure it out
nope it would be rjust(15)
Well, it depends on the number.
thought i had to add the right and left of the decimal places + a decimal which is how i got 19
if you dont mind clicking links, its a screenshot. do you know why the titles wont line up with the number inputs below it? (right align) http://prntscr.com/4p5hpz
Well, the , in the print. Hmmm.... missed that at first: ``` print('a','b','c') print('a'+'b'+'c') ``` Look at the spaces on those.
no spaces are all good
If you change the + to a , in the text print part it will add the extra space. If you use + in the numbers, well, it will work, but a full length number won't have a space after it.
yeah your right, just tried that
gotta love codes, 1 tiny tiny tiny thing wrong and it could ruin everything lol
Yep! The difference between a + and a , can be large. If you need the space, it is there. If you don't need it, it is gone. But if you get them reverse it causes things to look all funky.
yeah im gonna have to write that down, i like to keep little hints for myself to look back on
Here is another little test to see how things go: ``` print("Auto Number:") x=1234.5678 print(format(x,"15.3")) x=1234.567 print(format(x,"15.3")) x=1234.56 print(format(x,"15.3")) x=234.56 print(format(x,"15.3")) x=34.56 print(format(x,"15.3")) x=4.5678 print("\nFloating:") x=1234.5678 print(format(x,"15.3f")) x=1234.567 print(format(x,"15.3f")) x=1234.56 print(format(x,"15.3f")) x=234.56 print(format(x,"15.3f")) x=34.56 print(format(x,"15.3f")) x=4.5678 print("\nExponential:") x=1234.5678 print(format(x,"15.3e")) x=1234.567 print(format(x,"15.3e")) x=1234.56 print(format(x,"15.3e")) x=234.56 print(format(x,"15.3e")) x=34.56 print(format(x,"15.3e")) x=4.5678 ``` You can see it makes a difference on the output: ``` Auto Number: 1.23e+03 1.23e+03 1.23e+03 2.35e+02 34.6 Floating: 1234.568 1234.567 1234.560 234.560 34.560 Exponential: 1.235e+03 1.235e+03 1.235e+03 2.346e+02 3.456e+01 >>> ```
So the number and if you force the format or auto it makes a difference. And with floating point, when you reach the end it just ends. So that can make text over number colums not always line right up. But one solution would be to do everything in a character block.
Here, tweaked the original example I was working with: ``` stockPurch = 17043 stockCost = 7.9 total1 = stockPurch * stockCost stockPurch1 = 170 stockCost1 = 27.9 total2 = stockPurch1 * stockCost1 print('{0:10} | {1:10} | {2:10}'.format('Shares', 'Price', 'Total')) print('Shares'.rjust(10)+" | "+'Price'.rjust(10)+" | "+'Total'.rjust(10)) print('------------------------------------------') print(format(stockPurch,"10.0f")+" | "+format(stockCost,'10.2f')+" | "+format(total1,"10.2f")) print(format(stockPurch1,"10.0f")+" | "+format(stockCost1,'10.2f')+" | "+format(total2,"10.2f")) ``` And the output: ``` >>> Shares | Price | Total Shares | Price | Total ------------------------------------------ 17043 | 7.90 | 134639.70 170 | 27.90 | 4743.00 >>> ``` Well, almost. The output did not past in so good. Hehe. You can run it to see.
Then using center might be best because of the text based columns.
And getting cute: ``` stockPurch = 17043 stockCost = 7.9 total1 = stockPurch * stockCost stockPurch1 = 170 stockCost1 = 27.9 total2 = stockPurch1 * stockCost1 print("| "+'Shares'.center(10)+" | "+'Price'.center(10)+" | "+'Total'.center(10)+" |") print('+------------+------------+------------+') print("| "+format(stockPurch,"10.0f")+" | "+format(stockCost,'10.2f')+" | "+format(total1,"10.2f")+" |") print("| "+format(stockPurch1,"10.0f")+" | "+format(stockCost1,'10.2f')+" | "+format(total2,"10.2f")+" |") ``` For some sort of variable format you would build the string for the spacer bar. Use math to know how many - to put in.
Join our real-time social learning platform and learn together with your friends!