0

this is a simple intro to CS question but something isn't working right and i can't seem to find why:

num = int(input("enter a number:"))
print()
print("Number\t\tSquare\t\tCube")
print(" ",num,"\t\t",num**2,"\t\t",num**3)
num=num+1
print(" ",num,"\t\t",num**2,"\t\t",num**3)
num=num+1
print(" ",num,"\t\t",num**2,"\t\t",num**3)
num=num+1
print(" ",num,"\t\t",num**2,"\t\t",num**3)
num=num+1
print(" ",num,"\t\t",num**2,"\t\t",num**3)

this is meant to print the number, it's square and it's cube, for num and it's 4 consecutive numbers, always with 2 tabs between them:

enter a number:-8
Number      Square      Cube
 -8           64         -512
 -7           49         -343
 -6           36         -216
 -5           25         -125
 -4           16          -64

but when the square is a single digit answer the answer looks weird:

enter a number:-6

Number      Square      Cube
  -6         36          -216
  -5         25          -125
  -4         16          -64
  -3         9       -27
  -2         4       -8

it seems to ignore a tab in the printing process and print a space instead. anybody has a clue as to why?

AMC
  • 2,642
  • 7
  • 13
  • 35
  • 3
    Does this answer your question? [Python spacing and aligning strings](https://stackoverflow.com/questions/10623727/python-spacing-and-aligning-strings) – Ayush Garg Nov 24 '20 at 18:14
  • It doesn't *ignore* tabs. The starting point is earlier because there is one digit less so the end of the tab changes... – Tomerikoo Nov 24 '20 at 18:20
  • 1
    A tab is not a fixed amount of space; it's a jump to a fixed position past the cursor. – chepner Nov 24 '20 at 18:21
  • The tab character in a terminal emulator is just a tab stop, not a column delimiter. If you want to line up your output properly in a console you'll have to use one of a variety of solutions you'll find online. – TigerhawkT3 Nov 24 '20 at 18:22
  • For example, `print("\ta")` and `print(" \ta")` will give the exact same output. Tab is not equivalent to 4 spaces, it's advancing the cursor to the next 4-spaces jump – Tomerikoo Nov 24 '20 at 18:22
  • i'll expand, i can't show it here the console window shows me when i mouse select when a space was printed and a tab was printed, and when the square is printed right after it 1 space and 1 tab are printed instead of 2 tabs – Cipher_IL Nov 24 '20 at 18:23
  • `"\t"` is a single ASCII character, but how it gets *displayed* depends on the terminal. Most terminals display it as however many spaces are necessary to advance the cursor to the next tab stop, but it could also be displayed as some other single-width glyph, like ␉ (U+2409). In your case, if the cursor is already only one column before a tab stop, it prints 1 space to get you to that tab stop, then the next `"\t"` prints 4 spaces to get to the next tab stop after *that*. – chepner Nov 24 '20 at 18:30

0 Answers0