Getting input
Ask the user with input
input()waits for the user to type a line and press Enter.- It gives back what they typed. Store it in a variable.
- Join two strings together with
+.
name = input()
print("Welcome, " + name)
When your program waits
- When you press Run, the program stops at
input()and waits for you. - A highlighted box appears under the code: type here, then press Enter.
- Click the box, type your answer and press Enter. Then the program continues.

input always gives a string
- Whatever the user types comes back as a string.
- Even if they type
5, you get the text"5", not the number. - So
"5" + "5"is"55"(joined text), not10.
print("5" + "5") # 55 — joined text, not the number 10
Turn text into a number
int(...)turns text into a whole number:int("5")→5.float(...)turns text into a decimal:float("2.5")→2.5.- Convert first, then you can do maths.
age = int(input())
print(age + 1)
Common mistakes
input()always returns a string, so convert numeric input before doing maths: useint(...)for whole numbers orfloat(...)for decimal numbers.- The text inside
input("...")is the prompt shown to the user.
Now you try
- When you Run, type the input yourself in the box and press Enter.
- Check answer types the task's input for you, so just read it with
input(). - Read each value with its own
input(), and leave the brackets empty in tasks: the check compares everything your program prints, so prompt text such as"Name? "would count as output.
อินพุต → ประมวลผล → เอาต์พุต
Most programs follow one shape: get input, do something, show output.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read a name with input(), then print Hi, then the name then !. For the input Sam, the output should be Hi, Sam!.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read a whole number with input(), convert it with int(...), then print the number plus 100. For input 7, the output should be 107.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read a whole number, convert it with int(...), then print its สี่เหลี่ยม (the number times itself). For input 5, the output should be 25.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read the ความกว้าง, then the ส่วนสูง, each with int(input()). Print the area on one line and the perimeter on the next. For the inputs 3 และ 5 the output is Area: 15 และ Perimeter: 16.
คลิก Run เพื่อดูผลลัพธ์ที่นี่