0

How do I make an integer appear in a string

Okay, I'm trying to write a program that will take the age in integer but print the answer in a sentence. Like when I ask the question, 'how old are you', I get a prompt to input my age in years, convert it by multiplying by 365 days to get the result in days and , then I want to print the response in a sentence. Like 'You are 5500 days old. This is the program I wrote: x = input("How old are you: ") Age = int(x) y = (x * 365) print("You are y days old) I'm not getting the right answer, please help.

28th Feb 2017, 6:56 PM
Ukemeobong Akpan
9 ответов
+ 1
in python u can use format specifiers...eg., age = 10 print("Age is %d"%age) output: Age is 10
28th Feb 2017, 7:15 PM
Mohit Khokhar
Mohit Khokhar - avatar
0
print ("you are %d days old" % (y)) let me know if it doesn't work
28th Feb 2017, 7:04 PM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
Raj, it didn't work, there's an error saying that 'a number is required not string'
28th Feb 2017, 7:08 PM
Ukemeobong Akpan
0
let me check it on playground
28th Feb 2017, 7:10 PM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
ah! my bad, I didn't notice. mention int in input else it will take input as string do x=int (input ("enter age: ")) and print ("your age %d days" %(y)) it will work now
28th Feb 2017, 7:16 PM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
you did type conversion later. ..but multiplied with x, not with age...that's problem
28th Feb 2017, 7:18 PM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
That's good! Thanks Raj it work! Cheers
28th Feb 2017, 7:23 PM
Ukemeobong Akpan
0
cheers. ..good night :)
28th Feb 2017, 7:25 PM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
age=10 print("Your Age Is " + str(age)) number=50 number=str(number) print("Your number is " + number)
28th Feb 2017, 9:35 PM
LordHill
LordHill - avatar