Python for Kids: A Program That Talks to You with print and input (Code and Guide)

Python for Kids: A Program That Talks to You with print and input (Code and Guide)

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

ItemNotes
ComputerWindows, macOS or Linux
Python 3Free 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

  1. Print text on the screen: print() writes the text inside the brackets on the screen. Text must be inside double quotes.
  2. Ask a question and keep the answer: input() writes the question and waits for an answer. The answer is stored in a variable called name. The + sign joins pieces of text together.
  3. 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).
  4. Turn the number back into text: we cannot join a number and text with +. str() turns the number back into text.
  5. Use a variable again: color is a new variable. We use the name variable 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 + 1 to tell how old you will be next year

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

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

Bir Cevap Yazın