+ 1
Variables
You’re making a contact management system. The given program declares two variables, name and age. Complete the code to output "name is age years old", where name and age are the declared variable values. Guys, can someone solve this for me i’ve tried so many ways but still didn’t work not just using with + that doesn’t leaves spaces so any suggestions.
16 Respuestas
+ 3
Best method is to use f-string:
print(f"{name} is {age} years old")
Prefix the string with an f and put the variables you want to use in curly braces. No casting needed
+ 2
# in python
name = input()
age = input()
print(f"{name} is {age} years old.")
+ 1
Ah yes, as I thought. Your approach is not quite right. As I have mentioned before that you can't concatenate string with integer without converting the integer to string type first, so basically you can just try adding str() to the integer variable. Like so :
print(name + " is " + str(age) + " years old.")
Hope this helps. Also watch out for the proper punctuation in the output that the question asks for.
+ 1
In Python...
nm="Someone"
age=16
print("{} is {} years old".format(nm, age))
Hope it helped...!!
+ 1
value
0
If you tried, then show us what have you done so far
0
Can you post your attempt so I can figure out what methods you've tried? But maybe I can give you a small advice now, to concatenate string with integer you need to change the integer to string. You can achieve that by using the str() method that converts objects to string type.
0
Print ( name + “is” + age + “years old”) this is one of my attempts i forget the others
0
Sara_889 we cannot see the task, if we do not own pro version.
Anyway, try str() method mentioned by Dama Dhananjaya Daliman to convert ints to strings:
print(str(age)... )
You will learn typecasting in the next lessons, continue learning if you want more specific explanation
0
I did but it doesn’t leave space
0
Sara_889 if you want spaces in between the words just add them manually like if you do:
print(name+"is"+str(age))
>> Jamesis42
Instead do :
print(name + " is "+str(age))
>> James is 42
Hope you can see the difference.
0
print(f"{'James'} is {42} years old")
0
name = "James"
age = "42"
print(f"{'James'} is {42} years old")
0
#include <iostream>
using namespace std;
int main() {
string smiley = " @@@@ @@@@\n @@@@@@ @@@@@@\n @@@@@@ @@@@@@\n @@@@ @@@@\n\n\n\n@ @\n @@ @@\n @@@ @@@\n @@@@@ @@@@@\n @@@@@@@@@@@@@\n";
}