-1

Code

character_name = "Tom"  
character_age = "50"                                                        
is_male = False

print("There once was a man named " + character_name + ", ").  
print("he was " + character_age + " years old. ")

character_name = "Mike"  
print("He really liked the name " + character_name + ",")  
print("but didn't like being " + character_age + ".")

What I'm changing it to

character_name = "Tom"  
character_age = 50                                                        
is_male = False

print("There once was a man named " + character_name + ", ").  
print("he was " + character_age + " years old. ")

character_name = "Mike"  
print("He really liked the name " + character_name + ",")  
print("but didn't like being " + character_age + ".")

Error Code

/Users/praisekittens/PycharmProjects/Proj3/venv/bin/python /Users/praisekittens/PycharmProjects/Proj3/George.py  
There once was a man named Tom,  
Traceback (most recent call last):  
  File "/Users/praisekittens/PycharmProjects/Proj3/George.py", line 5, in <module>
    print("he was " + character_age + " years old. ")  
TypeError: can only concatenate str (not "int") to str

Process finished with exit code 1

Thoughts

I am brand new to coding and am following a four year old tutorial on Youtube from freeCodeCamp.org and everything had worked absolutely perfect until this part. It appears to me that the removal of the quotations marks is causing an issue with concatenating an int to a str.

When he takes the quotations away I do see that both character_age fields get highlighted yellow indicating an error. From how he is talking he makes me think he was able to run this code and have it work.

Questions

Can my code work without the quotations around the 50?
Why is he suggesting to do this in the first place? I was expecting some benefit from separating str and int and instead ran into this mildly frustrating error
What is the best code to use for this scenario?

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • "Why is he suggesting to do this in the first place?" We can't see the tutorial and we can't read the author's mind. If you are trying to follow a tutorial and you find that the code shown routinely gives errors that are not explained in the tutorial itself, **use something else**. – Karl Knechtel Sep 28 '22 at 20:18

3 Answers3

0
print(f"There once was a man named {character_name}, he was {character_age} years old.")
character_name = "Mike"  
print(f"He really liked the name {character_name},but didn't like being {character_age}.")

You can try something like this so character age can be either string or integer ('15', or 15).

  • 2
    You don't need the call to `str`. If you use an f-string a simple `f"He really liked the name {character_name},but didn't like being {character_age}."` is enough. – Matthias Sep 28 '22 at 18:42
0

You can convert the integer into a string using str() In your code you would put:

print("he was " + str(character_age) + " years old. ")

print("but didn't like being " + str(character_age) + ".")

And as an extra tip you can also use python f strings for print formatting. These were introduced in python 3.6. Here is an example.

print(f'he was {character_age} years old. '

This lets you format without having to use string concatenation all the time. Cheers.

Sterling
  • 432
  • 2
  • 9
0

Can my code work without the quotations around the 50?

Yes. You only have to cast the variable to a different type using str(your_variable) or an f'' string.

print(f"He really liked the name {character_name}, but didn't like being {character_age}.)" 

Why is he suggesting to do this in the first place?

As it is mentioned here Python is extremely typed, and will avoid doing implicit type conversions.

What is the best code to use for this scenario?

It depends on you. You have three options to fix this:

  • Convert the integer variable to a string before concatenating
  • Use a comma in the print() statement to concatenate
  • Use an f'' string (f-string).
Kensy
  • 112
  • 8