Reputation: 45
python newbie here. I'm trying to print a 5 line pyramid of text based off the user's input if the string is even. I'm having trouble making the pyramid centre without hardcoding the spaces in. In line 2 I'm getting "TypeError: unsupported operand type(s) for -: 'str' and 'int' If len is returning the length and int and I'm multiply the space by that number how is this an error? Thank you :)
userString = input( "Please enter a string with a value of 7 or less characters: " )
space = ' ' * int( len( userString ) ) - 1
left_side = userString[:len( userString ) // 2]
right_side = userString[len( userString ) // 2:]
def pyramid( left, right ):
print( space + left_side + right_side )
print( space + left_side * 2 + right_side * 2 )
print( space + left_side * 3 + right_side * 3 )
print( space + left_side * 4 + right_side * 4 )
print( space + left_side * 5 + right_side * 5 )
Upvotes: 0
Views: 164
Reputation: 11476
*
has higher precendence than -
, so you must wrap them in parenthesis
space = ' ' * (len(userString) - 1)
Upvotes: 0