Bu yazıyı Türkçe okuyun · Leer en español
What does this project do?
In the second episode of the Python for Kids series, we write a program that asks your name, talks to you by name and works out how many days you have been alive. We learn print, input, variables, int and str. For ages 10 and up who are new to Python.
- Age group: 10 and up
- Level: Beginner
- Time: About 30 minutes
- What you will learn: print, input, variables, int and str
What you need
| Item | Notes |
|---|---|
| Computer | Windows, macOS or Linux |
| Python 3 | Free from python.org; IDLE comes with it |
Code
In IDLE, open a new window with File → New File, type the code, save it as talking_program.py and press F5 to run it.
# A Program That Talks to You - ozandamci.com
print("Hello! I am Python.")
name = input("What is your name? ")
print("Nice to meet you, " + name + "!")
age = input("How old are you? ")
age = int(age)
days = age * 365
print(name + ", you have been alive for about " + str(days) + " days!")
color = input("What is your favorite color? ")
print(color + " is a lovely color. See you, " + name + "!")
Step by step
- Print text on the screen:
print()writes the text inside the brackets on the screen. Text must be inside double quotes. - Ask a question and keep the answer:
input()writes the question and waits for an answer. The answer is stored in a variable calledname. The+sign joins pieces of text together. - Turn text into a number:
input()always gives text, such as “10”.int()turns it into a number so we can multiply it by 365. The multiplication sign is*(a star). - Turn the number back into text: we cannot join a number and text with
+.str()turns the number back into text. - Use a variable again:
coloris a new variable. We use thenamevariable once more at the end of the program.
Try it yourself
- Count the hours: multiply the days by 24 and print how many hours you have been alive
- Add a new question: make it ask for your favorite animal
- Next year: use
age + 1to tell how old you will be next year





“Python for Kids: A Program That Talks to You with print and input (Code and Guide)” için 2 yorum