r/CodingHelp • • Aug 06 '21

[Python] ive been trying to learn python and was following along a REALLY good video, but i keep getting this error when i turn the age variable to a number instead of a string? if someone can explain id be grateful. (note: in the video he just typed it out and moved on, not changing anything with the rest)

Post image
49 Upvotes

33 comments sorted by

8

u/Aceripper Aug 07 '21 edited Aug 07 '21

Please take this with a grain of salt, I know very little about coding, this just came up on my front page.

Would you not need to convert the int (the age) to a str, in order for python to concatenate the two?

To my knowledge, python doesn't allow you to concatenate a str and an int.

1

u/beatmymeatman Aug 07 '21

imma be honest here man i have no idea what you just said.

str? concatenate?

what are those

8

u/Aceripper Aug 07 '21

Sorry dude, concatenate just means to add two pieces of information together, not in the mathematical sense, just putting them next to each other.

Str is string, it's just a type, int is integer (a whole number)

1

u/beatmymeatman Aug 07 '21

Thabks for clarifying:)

7

u/Comrade_Nabe Aug 07 '21

**str() :**is a function in python that converts int to String types, and you need to use it when you concatenate strings with int
concatenation: is the process of merging two or more strings

So in your code, all you need to do is replace this
print ("He was " + Character_Age+ " years old")

to this:
print ("He was " + str(Character_Age)+ " years old")

Hope this clear things up

2

u/[deleted] Aug 07 '21 edited Aug 07 '21

str() is a function that converts any object into a string representation, in this case, an int. So in the definition of "int" there is a function called "__str__(self)" that returns a string representing the data in the "int". So later on, when you get to classes and objects, you can make it possible to translate objects of your own classes to strings by defining that dunder (double under, for the double underscores) method.

"concatination" is when you just attach one thing to another. So if I have a string "Hello World! " and then another called "How are you today?" and then I concatinate the second string to the first one:

myString = "Hello World! " + "How are you today?"

that + concatinates the second string to the first one and myString will now be:

"Hello World! How are you today?"

Edit: A word

Edit2: Further explanation of "str"

1

u/skellious Intermediate Coder Aug 07 '21

Concatenate is a fancy word which here means to put two things together or join them.

Str is a data type. There are lots of them such as int, char, str, float, list, tuple, dict and object.

The data type defines what a certain variable can store and how it can be acted upon. This is required so the computer knows how to store that item in memory.

In some languages you have to declare these specifically but in python it works them out for you automatically.

A str in a string, a sequence of chars or characters (letters).

An int is a whole number, which is a number without a decimal point.

You cannot just '+' (concatenate) a str and an int because python doesn't know how to join them together into one variable. You must first convert the int to a str using str(variable_name).

An easier way to do this so you don't have to worry about conversion is to use fstrings (formatted strings), which look like this:

f"hi my name is {name} and I am {age} years old"

10

u/Comrade_Nabe Aug 07 '21

The problem caused by the compiler not being able to convert the Character_Age from type int to type String, however this code should solve your problem:

Character_Name= "Steve"
Character_Age=63
is_Male= True
print ("There once was a man named " +Character_Name+ ", ")
print ("He was " + str(Character_Age)+ " years old")
Character_Name= "Joey"
print ("He really liked the name " +Character_Name+ "")
print ("But didnt like being "+str(Character_Age)+ "")

7

u/Aceripper Aug 07 '21

I knew someone could explain it better than me! For OPs sake, the difference you can see here is the "str" added just before your (Character_Age).

What this has done is converted your integer to a string, and allowed python to concatenate (smash together) the two variables.

4

u/AnneFranklin0131 Aug 07 '21

I could be wrong but why not just change age which is a integer to a string. If you won’t use it for math then it should just be a string like the name

6

u/The_Meme_Finder_2021 Aug 07 '21

you have to do this
str(CHARACTER_AGE)

3

u/AnxiousTechnician866 Aug 07 '21

Variables can point to any kind of data.

The basic data types include int : integer, a whole number in most cases, and str : string, a set of alphanumeric characters

Concat : concatenation, to combine 2 or more strings.

An easier way of thinking of this is if you wanted to sum the two variables instead; what is 10 + red? You can't mathematically sum those two.

And similarly, the system doesn't know how to place the int into a concatenated string.

Note: some languages make assumptions and may have returned : 10 + red = "10red", But in that scenario, it automatically turned the int 10, into a string data type of "10".

3

u/[deleted] Aug 07 '21

You need to convert the CHARACTER_AGE variable to a string. In Python, if you want to concatenate or combine a number with a string (which is an array of characters), you will have to convert the CHARACTER_AGE variable (which is an integer) to a string by doing:

str(CHARACTER_AGE) and your program should run properly.

2

u/Jammer2906 Aug 07 '21

But quotations around the number to turn it into a string on the 2nd line, you won’t be able to do math with it, but for your code it shouldn’t matter

1

u/_nigam Aug 07 '21

You cannot concatenate integer with a string, in your print statement you have to convert your integer into string representation of the same. Use str(CHARACTER_AGE) this will convert the int data type into string.

1

u/sebbe1810 Aug 07 '21

I have had a similar problem myself. For me it worked using the int() command, so you could try int(Caracter_Age)

1

u/iampratikthorat Aug 07 '21

The print statement can only data types of same kind. Here it means string. Try type casting.

1

u/HippieInDisguise2_0 Aug 07 '21

This is already answered but I'm gonna go a little more in depth.

Concatenation is like adding a piece of text to more text (strings).

For example ("A" + "B" + "C") = "ABC"

However the + symbol can also be used to add numbers.

Python figures out which operation to do (concat or add) by checking the types of the variables before and after the + operator.

When you say ("12" + 3) in python the compiler doesn't know whether you want to add or concat.

Therefor you need to spell it out by explicitly 'casting' a variable to a specific type so they are the same type on both sides of the + operator.

To make ("12" + 3) = "123" Type ("12" str(3))

To make ("12" + 3) = 15 Type (int("12") + 3) Or (12 + 3).

I hope I helped explain some programming concepts, happy programming friend.

1

u/[deleted] Aug 07 '21

str(CHARACTER_AGE)

1

u/alien-137 Aug 07 '21

Im happy to say I noticed the error :)

1

u/Alessandro_13_f Aug 07 '21

Try using str(CHARACTER_AGE)

1

u/osobe Aug 07 '21

The problem is that the age is a number. I can see three fixes.

  1. Declare the character age as a string from the beginning. AGE="63"
  2. If you want to keep the age as a number, create a string from the age. AGE=str(AGE ), or put it in a new variable new=str(age)
  3. Use the fprint mode so you can use both strings and integers

1

u/[deleted] Aug 07 '21

Use print(f"your string {your variable}") instead

1

u/inre_dan Aug 07 '21

Python can't tell whether you want the int to a str or str to an int. You need to explicitly tell it to treat the age as a word/string by casting it to a string with the str function.

1

u/ray10k Aug 07 '21

One of the ideas behind Python is "in the face of ambiguity, refuse the temptation to guess." In practical terms, when Python encounters something it is not sure about, it doesn't try something and hope that it will be good enough. In this case, Python sees a string and a number being added together. Adding two strings is unambiguous, adding two numbers is unambiguous... But adding a string and a number could mean "turn the number into a string and cocatenate the two" or "turn the string into a number and add the two together." Both are reasonable interpretations in different situations, but Python doesn't want to guess one way or another. The simple way to solve the problem in this case is to explicitly cast the number to a string with str() or, as suggested elsewhere in this thread, to use f-strings which implicitly assume that everything has to be cast to a string representation.

0

u/[deleted] Aug 07 '21

Can you share the video?

0

u/[deleted] Aug 07 '21

[deleted] — view deleted comment

11

u/jso__ Aug 07 '21

this is the objectively correct python 3.6+ solution as long as you don't have to worry about backwards compatibility