Night 3: Talk to your pet
Tonight: talk to your pet
- Your pet has a name. Now it wants to know your name.
- You will learn one thing:
input().
Words for tonight
| Word | Meaning |
|---|---|
| input | what the user types into the program |
| user | the person who uses the program |
| wait | stop and do nothing until something happens |
input() waits for the user
input()stops the program and waits.- The user types something and presses Enter.
- Keep the answer in a variable.
print("What is your name?")
name = input()
print("Hello,", name)
- Press Run. A box opens. Type your name and press Enter.
The answer is always text
input()always gives you text, even if the user types7.- Tomorrow you will see how to turn text into a number.
Ask, wait, answer
- Many programs work like this:
-
- Ask a question with
print.
- Ask a question with
-
- Wait for the answer with
input().
- Wait for the answer with
-
- Answer with
print, using the variable.
- Answer with
print("What is your favourite food?")
food = input()
print("Yum!", food, "is great!")
Now you try
- Do the tasks below. Press Run and type an answer.
- Then press Check answer. The checker types some answers for you.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Your pet asks your name. Read it with input(). Then print Nice to meet you, and the name. For the name Sam, the output is Nice to meet you, Sam.
Klik Jalankan untuk melihat output di sini.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Your pet asks: What colour? Read the answer with input(). Then print I will paint my house and the colour. For blue, the last line is I will paint my house blue.
Klik Jalankan untuk melihat output di sini.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Read dua answers with two input() lines: first a name, then a city. Then print one line like Sam lives in Ningbo.
Klik Jalankan untuk melihat output di sini.