![]() |
|
CHAPTER 5 — INPUT & OUTPUT - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 5 — INPUT & OUTPUT (/showthread.php?tid=223) |
CHAPTER 5 — INPUT & OUTPUT - Leejohnston - 11-15-2025 Chapter 5 — Input & Output Your programs can now talk to the user Up until now, your programs have only displayed information. In this chapter, you will learn how to: • ask the user for information • store what they type • respond to them • create interactive programs Input + output is the foundation of almost every real application. --- 5.1 Output: print() You already know print(): Code: print("Hello!")But now we go one step further. Output becomes powerful when combined with input. --- 5.2 Input: Asking the User Questions To get information from the user, use: Code: input("Your question here: ")Example: Code: name = input("What is your name? ")If the user types: Code: MiaThe output is: Code: Hello, Mia--- 5.3 Input Always Gives You a String Important: Whatever the user types comes in as TEXT (a string). Example: Code: age = input("How old are you? ")Python sees “16” as text, not a number. To convert to a number: Code: age = int(input("How old are you? "))Now it works. --- 5.4 Converting User Input Use: • int() → convert to whole number • float() → convert to decimal • str() → convert to text Examples: Code: x = int(input("Enter a number: "))--- 5.5 Combining Input + Output Try this: Code: name = input("What is your name? ")Your program now behaves like a little conversation. --- 5.6 A Simple Calculator Program This program asks for two numbers and adds them: Code: a = float(input("Enter the first number: "))This is your first interactive tool. --- 5.7 Building a Custom Greeting Try this: Code: name = input("Your name: ")This combines variables, input, and arithmetic. --- 5.8 Avoiding Common Input Mistakes Common mistake: Code: number = input("Enter a number: ")If the user enters 3, the output is: Code: 33333Because Python repeats the string. You MUST convert: Code: number = int(input("Enter a number: "))--- 5.9 Exercise — Build a Personal Greeting App Create a file called greet.py. Ask the user for: • their name • their age • their city Then print a message like: Code: Hello Mia! You are 16 from Liverpool.Try to format it neatly. --- 5.10 Small Challenge Make a program that asks the user for: • a price • a discount percentage Then calculate the discounted price and display it. Hint: Code: discount = price * (percent / 100)--- 5.11 Chapter Summary • input() asks the user a question • input ALWAYS returns a string • use int() or float() to convert numbers • combine input and output for interactive programs • print() displays results and responses • you can now build your first simple tools Next: Chapter 6 — Strings This is where you learn how to manipulate text — a crucial skill for all beginners. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |