0

So I've got an assignment for my CS class. It's very basic but I'm interested in this class and I wanted to make the code a bit "fancier" I guess. I've been googling to see how I can print the results with some ASCII art and some user inputs below. I've got the art down, and the input parts nearly ready!

I want them to line up nicely. I want to limit the user input to a certain amount of characters for the first name input, and then have the second one print in a specific place. I hope what I'm typing makes sense!

For context, the assignment is:

Given a person's name, their age, the name of their dog and their dog’s age (in human years), display a picture, similar to the following, with the names displayed and the ages of both the person and the dog in “dog years” printed at the bottom of the picture.

So the code I have so far goes like this.....:

#Assignment #1, I love ascii art so I hope you don't mind that I changed things up a little!
InputName = input("Please enter your name: ")
InputHumanAge = int(input("Please enter your age: "))

DogName = input("What is your dog's name? ")
InputDogAge = int(input("How old is your dog in human years? "))

HumanYears = InputHumanAge*7
DogYears = InputDogAge*7

print('      ________ ')
print('    /          \             ___       ___ ')
print('  /             \           |   \_____/   | ')
print(' / /  \______\__\ \        /  |\/     \/|  \ ')
print(' | |  O      O |  |        \_/ | /\ /\ | \_/ ')
print(' / |     __    /  |            |_\/ \/_| ')
print(' \  \ _______ /  /            /   \o/   \ ')
print('  |   __|  |__  |             \___/O\___/ ')
print('  '+ InputName +'                                '+ DogName +' ')
print('  '+ str(HumanYears) +'                       '+ str(DogYears) +' ')

My problem right now is that the text underneath the dog doesn't line up the way I want. Any help is appreciated <3 Thank you!

wjandrea
  • 28,235
  • 9
  • 60
  • 81
snowbuns
  • 31
  • 5
  • This is either an extremely simple problem (just print a number of spaces minus the length of the `InputName`) or a very hard one (compute the width of the `InputName` when printed, depending on the font used for printing). But considering you are doing ASCII art, I'm assuming the font will be fixed width, so it's really as simple as printing a string with a number of spaces that's equal to some number minus the length of the name. Not really worthy of an SO question? – Grismar Jan 23 '20 at 01:55
  • Thank you so much! As I said I'm very new. I just didn't know where to go to ask people these things... – snowbuns Jan 23 '20 at 01:59
  • Your question is fine by itself, you did a good job of providing an example and pointing out the problem, but you should probably try writing a solution to the problem and only come here if you cannot get the solution to work. From the example, it appears you realised what the problem was, but didn't really try to solve it yourself. You learn more and get better answers if you try something yourself first. – Grismar Jan 23 '20 at 02:01
  • This is called alignment, and Python has a few options built in, for example [`str.ljust()`](https://docs.python.org/3/library/stdtypes.html#str.ljust) or `<` for left alignment in a [format spec](https://docs.python.org/3/library/string.html#formatspec). BTW welcome to Stack Overflow! Check out the [tour] and [ask]. – wjandrea Jan 23 '20 at 02:02
  • @Grismar Why on earth are you talking about manually calculating widths when Python can do alignment automatically? e.g. `str.ljust` – wjandrea Jan 23 '20 at 02:04
  • Does this answer your question? [How do I align text output in python?](https://stackoverflow.com/questions/17091446/how-do-i-align-text-output-in-python) or [Python spacing and aligning strings](https://stackoverflow.com/q/10623727/4518341) or [How to Left Justify part of a String in python?](https://stackoverflow.com/q/13809053/4518341) – wjandrea Jan 23 '20 at 02:06
  • @Grismar Thank you for your patience with me, you have a good point. I usually learn things myself by finding my exact questions answered in placed like SO, I've been having a hard time wording it so I figured I would just ask it myself. I had a general idea of what I wanted but no idea how to go about it...you summed it up perfectly! I'm off to do some more googling, thank you <3 – snowbuns Jan 23 '20 at 02:09
  • @wjandrea "why on earth"? Because clearly we're dealing with someone learning to program and that's the problem they should identify. Handing them the library function that does the work does little to help, even though I'd use it myself. – Grismar Jan 23 '20 at 02:10
  • Oh wow while I was typing all of your replies came in! Thank you so much everybody, aligning text is exactly what I meant! I feel like I'm having a really dumb night with not being able to word things right. I am so thankful for all the help :') <3 – snowbuns Jan 23 '20 at 02:12
  • @Grismar Fair point, it's useful to understand what's going on under the hood, but IMO Python is fundamentally a language where you don't need to understand that, and it's strongly encouraged to use existing tools instead of rolling your own. – wjandrea Jan 23 '20 at 02:26

2 Answers2

0

Use the .format() to assign a fixed amount of space to the words. Stackoverflow has all the info you need about using .format(), this will assure that the same amount of space is given in between the words even if 'Inputname' gets longer

Try and use

print("{0:28} {1:20}".format(InputName, DogName))

instead of

print('  '+ InputName +'                                '+ DogName +' ')
0

If you are using Python 3.6+, you can use f-strings like this:

print(f'  {InputName:<28s}{DogName}')
print(f'  {HumanYears:<28d}{DogYears}') 

Instead of:

print('  '+ InputName +'                                '+ DogName +' ')
print('  '+ str(HumanYears) +'                       '+ str(DogYears) +' ')

To move dog's attributes left or right, decrease or increase, respectively, that 28 value in the f-strings.

That <28s means align on left and fill right with spaces up to a 28 characters wide. Ditto for <28d but for integers.

accdias
  • 5,160
  • 3
  • 19
  • 31