113k views
4 votes
Instructions

myinfo.py
1 # Put your code here
Write and test a program that accepts
the user's name (as text) and age (as a
number) as input
The program should output a sentence
containing the user's name and age.
An example of the program input and
output is shown below:
Enter your name: Alex
Enter your age: 23
Alex ls 23 years old.

2 Answers

2 votes

Final answer:

To write a Python program that takes a user's name and age and outputs a sentence, you prompt for input, convert the age to an integer, and then use string formatting to put together the output sentence.

Step-by-step explanation:

To create a program that accepts a user's name and age and then outputs a sentence with that information, you can write a simple program in Python. Below is an example of how this can be done.

Step 1: Prompt the user for their name using the input() function.

Step 2: Prompt the user for their age. Make sure to convert the age from a string to an integer using the int() function.

Step 3: Use string formatting to construct the sentence that includes the user's name and age.

Here's a sample code snippet:

# Ask for the user's name
name = input('Enter your name: ')
# Ask for the user's age
age = int(input('Enter your age: '))
# Output the sentence with the name and age
print(f'{name} is {age} years old.')

This program will display the prompt, accept input from the user, and then output the sentence as requested.

User Vlad Morzhanov
by
6.1k points
3 votes

Answer:

#here is code in python

#read the name

name=input("Enter your name:")

# read the age as integer

age=int(input("Enter your age:"))

#print the output in the required format

print("{} is {} years old.".format(name,age))

Step-by-step explanation:

Read the name from user and assign it to variable "name".Then read the age from user and assign it to variable "age". In the next line print the output as required using the format function.

Output:

Enter your Sam

Enter your age:24

Sam is 24 years old.

User GreatBlakes
by
6.5k points