Input, Types, Error Handling & GitHub
User input, Python types and basic conversions, simple error handling with try/except; remotes and GitHub basics.
Learning Objectives
- Read user input with
input()and inspect values withtype(). - Perform basic type conversions using
int(),float(),str(), andbool(). - Handle common input errors using
try/except(e.g.,ValueError,ZeroDivisionError). - Set up a remote, connect a local repo to GitHub, and push the first commit.
Session Plan (theory)
- Quick recap (local Git)
Working directory, staging, commits (git status/add/commit/log). - Python: input & types
-
input()returns strings; check withtype(value). - Casting:
int("42"),float("3.14"),str(123),bool("any"). - Trim before casting with
.strip().
-
- Basic error handling
-
try/exceptfor invalid numeric input (ValueError). - Guard division by zero (
ZeroDivisionError) and unknown operators. - Provide clear user feedback messages.
-
- Git remotes & GitHub
- Create a remote on GitHub.
- Link and push:
git remote add origin <repo-url> git branch -M main git push -u origin main - Basics of
git clonefor a new machine.
Hands-on
- Continue from Week 1 repo (or start a fresh one).
- Mini Project: Console Calculator v1.1 (with try/except)
- Ask for two numbers and an operator (
+,-,*,/). - Convert inputs safely with
try/except ValueError. - Handle division by zero with
try/except ZeroDivisionError. - Print a formatted result using an f-string (
:.2f).
Starter snippet ```python a_str = input(“First number: “).strip() b_str = input(“Second number: “).strip() op = input(“Operator (+, -, *, /): “).strip()
try: a = float(a_str) b = float(b_str) except ValueError: print(“Invalid number. Please enter numeric values (e.g., 3 or 3.5).”) raise SystemExit(1)
try: if op == “+”: result = a + b elif op == “-“: result = a - b elif op == “*”: result = a * b elif op == “/”: result = a / b # may raise ZeroDivisionError else: print(“Unsupported operator. Use one of: +, -, *, /”) raise SystemExit(1) except ZeroDivisionError: print(“Error: division by zero.”) raise SystemExit(1)
print(f”Result: {a:.2f} {op} {b:.2f} = {result:.2f}”)
- Ask for two numbers and an operator (