Excuse me if the title is a bit misleading but I was a bit unsure on a relevant title name.
I trying to make a text box around some like the snippet here:
Die1 = random.randint(1,10)
Die2 = random.randint(1,10)
Die3 = random.randint(1,10)
if Die1 > Die2:
print ("+------------------------------+")
print ("| Die 1: | | Die 2 |")
print ("| %d | | %d |" % (Die1,Die2))
print ("+------------------------------+")
else:
print ("+------------------------------+")
print ("| Die 1: | | Die 2 |")
print ("| %d | | %d |" % (Die1,Die2))
print ("+------------------------------+")
This is all dandy if i get a result of 1 digit numbers :
+------------------------------+
| Die 1: | | Die 2 |
| 2 | | 6 |
+------------------------------+
However twp digit numbers will give me this:, another space will be pushed out for two tens.
+------------------------------+
| Die 1: | | Die 2 |
| 10 | | 2 |
+------------------------------+
I could do something like:
if Die1 > Die2 and Die1 or Die2 == 10:
print ("+------------------------------+")
print ("| Die 1: | | Die 2 |")
print ("| %d | | %d |" % (Die1,Die2))
print ("+------------------------------+")
Etc... but i'm sure there is a more practical way of doing this.
Is there a way to use a single digit to represent the &s ?, and/or and some padding to allow for a second digit if its needed.
I Had a look at the Python Doc's on the matter and i read some padding options for 0's but nothing that could help me directly.
You can use the string.format function to pad with spaces: (note that this function works in both python 2 and python 3)
"| {0: >2d} | | {1: >2d} |".format(Die1,Die2)
Inside the {}s, the 0 and 1 represent which argument is being referenced, the space after the colon is the padding character, > means to right-justify the text, the 2 is the width of the text, and the d specifies that the argument is an integer.
Example output:
| 2 | | 9 | | 10 | | 12 |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With