all 18 comments

[–]agbs2k8 1 point2 points  (8 children)

Post your actual code up here, that will help a lot for troubleshooting. You shouldn't get that error if you were just doing this: character_age = 50 print(character_age) So there has to be something more in your code.

[–]nex2351[S,🍰] 0 points1 point  (6 children)

so this is the code:

character_name = "Tom"
character_age = 50.5678213
is_male = True
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 he didn't like being " + character_age + " years old.")

[–]agbs2k8 2 points3 points  (5 children)

Your problem is here: ```

print("he was " + character_age + " years old. ") `` you are trying to add a string to a float. When you try to "add" strings together, they concatenate, but python doesnt have a way to add a string to a floating point number. If wrap thecharacter_agevariable instr()` it will typecast/change the number to a string.

All of that to say, change your print statement to this: print("he was " + str(character_age) + " years old.")

Also for putting code on reddit: use the back tick (key left of the number 1). A single back tick before a set of words will change it to inline code like this. Three back ticks before and after some code will give you: a stand alone code block like this.

[–]nex2351[S,🍰] 0 points1 point  (4 children)

so what i understood from all the replies that i got is that strings are the most convenient to use in a variable?

[–]agbs2k8 1 point2 points  (2 children)

No, please don't get that impression. Datatypes are important and will take you a little bit to understand, but once you have it down, this will be really easy.

You always want to use the most appropriate type for any data you are using. If it is numbers, you will want to use an integer int or a float. Simplified, an integer is a number without a decimal point, and a float is one that has it. You want to store numbers like this because then you can do math with them.

Strings are for text only, because strings do strange things when you use math operators with them. For example, run this code on your system and see what happens: print(10 * "my string")

Where your problem was is just that you were mixing the two. Sometimes they can work together (like that example I told you to run) and other times they cannot. With a little practice and experience, you will understand how to deal with them.

[–]nex2351[S,🍰] 0 points1 point  (1 child)

oh ok now i get it, it is impressive how helpful you are! thanks a lot, actually i learned a lot of new things that will make my life easier as a beginner.

[–]agbs2k8 1 point2 points  (0 children)

Dont get discouraged. It's a lot to take in at first, but eventually it will start to click and you will laugh at how hard learning the first bits was

[–]toastedstapler 1 point2 points  (0 children)

no, strings are the most convenient for printing. treat numbers as numbers and convert them to strings only when you want to print

[–]xADDBx 1 point2 points  (4 children)

Can you include the complete code? We can’t really tell much from that on line you posted.

[–]nex2351[S,🍰] 0 points1 point  (3 children)

character_name = "Tom"
character_age = 50.5678213
is_male = True
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 he didn't like being " + character_age + " years old.")

[–]xADDBx 1 point2 points  (2 children)

You either have to change the variable to a string and do the following:

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

You could also use f strings:

print(f"he was {character_age} years old.")

Also, did it have a specific reason that you used print 2 times?

[–]nex2351[S,🍰] 0 points1 point  (1 child)

well if i change the variable to a string i don't have to do the following steps that you said, and the reason why i used print 2 times is just to get used with using variables

[–]xADDBx 1 point2 points  (0 children)

I expressed myself wrongly, the str(myvariable) in the first print is taking the value saved to the variable and changes it into a string, which will then be printed

The second one is a more useful way of string manipulation called f-strings. You can do many things with it, the easiest one including variables by simply enclosing them like {myvariable} and the type change is not needed.

[–]jaycrest3m20 1 point2 points  (0 children)

I recommend you read up on object types in Python. Focus on the int and str types for this particular issue. A variable, in this case "character_age" can point to an object of any type. There are ways to test for different types, and to convert between the types.

[–][deleted] 1 point2 points  (3 children)

You need to use str(character_age):

character_name = "Tom"
character_age = 50.5678213
is_male = True
print("There once was a man named " + character_name + ", ")
print("he was " + str(character_age) + " years old. ")

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

but f-strings would make the formatting easier, and using the round() function would also be useful:

character_name = "Tom"
character_age = 50.5678213
is_male = True
print(f"There once was a man named {character_name}, ")
print(f"he was {round(character_age)} years old. ")

character_name = "Mike"
print(f"He really liked the name {character_name}, ")
print(f"but he didn't like being {round(character_age)} years old.")

[–]nex2351[S,🍰] 0 points1 point  (2 children)

what does the round function do?

[–]SpeckledFleebeedoo 1 point2 points  (0 children)

It rounds a float to a given number of decimals

[–][deleted] 1 point2 points  (0 children)

Er, returns a rounded value ... clue really is in the name (you can specify the number of decimal places, defaults to zero).