New, i need some help lease

def money (mula,dime):
 return  mula+dime

final = money(3,5)
print final 
if final == 8:
  print "whyyy" .center(54)
  if final == 8:
    print "true" .center(54)
    print final * 3 .center(65)

how can i get my final answer(print final *3 .center(65)) to be in the center. Everytime i try to run it, it just dosent work.

Hello :slight_smile: Welcome to the forum!

Your code results in:

AttributeError: ‘int’ object has no attribute ‘center’

final * 3 is an int, but the center method is defined only for strings (that is why "whyyy".center(54) results in a correct result.

To cast an int to a string you can use function str. Here is the documentation -> https://docs.python.org/2/library/functions.html#str.

4 Likes

so what should i do?

like can you show me ho to get it in the center

Let’s look at a different string method, as a loose analogy:

str.replace(s1, s2) returns a new string with each instance of the substring (or character) s1 replaced by s2, for example:

my_str = 'abcde'
new_str = my_str.replace('c', 'x')

print(my_str)
print(new_str)

Output:

abcde
abxde

Now, I changed my mind, and I want to replace ‘c’ with 77 …

new_str = my_str.replace('c', 77)

print(my_str)
print(new_str)

Output:

Traceback (most recent call last):
  File "C:\Users\Patrick\path\to\test.py", line 6, in <module>
    new_str = my_str.replace('c', 77)
TypeError: replace() argument 2 must be str, not int

Ah! …It seems that I need the int 77 to become a string, ‘77’

new_str = my_str.replace('c', str(77))

print(my_str)
print(new_str)

Output:

abcde
ab77de
3 Likes

final = money(3,5)
print final
if final == 8:
print “whyyy” .center(54)
if final == 8:
print “true” .center(54)
print final * 3

so how would i get the print final* 3 to be in the middle right away when it prints. because when i click run it showes the answer(24) all the way to the left side. how can i get it to print in the middle.

1 Like

Hi, @brainpow A few points:

  1. I don’t know exactly what you are trying to do. Would you post a link to the problem so that we are all talking about the same thing?

  2. Along with that, please post an example of how you would like the output to look. Use the </> icon in the menu bar at the top of the text box, and type your desired output where you see “Preformatted text” highlighted.

  3. When you post your code, also make use of the same </> icon. Go to a free line, click the icon, and paste your code in where it says “type or paste code here”

If you do these things, it will be much easier to help you.

3 Likes

As factoradic explained above, final * 3 will result in an integer, so you need cast it to a string first before you can use the string method .center() on it.

You can do that by changing your last line as follows:

print(str(final * 3).center(65))
2 Likes