r/CodingHelp • u/beatmymeatman • 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)
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
1
u/Radicat777 Aug 07 '21
Video link please
1
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
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
1
1
1
u/osobe Aug 07 '21
The problem is that the age is a number. I can see three fixes.
Declare the character age as a string from the beginning. AGE="63"
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)
Use the fprint mode so you can use both strings and integers
1
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
0
Aug 07 '21
[deleted]
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
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.