Functions
Lists
Files
Strings
Bonus
100

Write a function named greet that prints “Hello, world!” to the screen. Then call it.

def greet():

    print("Hello, world!")

greet()

100

Create a list colors with 5 colors. Print each one.

colors = ["red", "blue", "green", "yellow", "purple"]
for color in colors:
  print(color)

100

Ask user for name, write it to name.txt.

name = input("Enter your name: ")
with open("name.txt", "w") as f:
  f.write(name)

100

Ask user for first & last name. Print full name.

first = input("First: ")
last = input("Last: ")
print(first + " " + last)

100

Store 3 math functions in list, apply to two numbers.

def add(x, y): return x + y
def subtract(x, y): return x - y
def multiply(x, y): return x * y
funcs = [add, subtract, multiply]
x, y = 5, 2
for f in funcs:
  print(f(x, y))

200

Write a function square(num) that returns the square of num. Ask the user for input.

def square(num):
  return num ** 2
n = int(input("Enter a number: "))
print(square(n))

200

Make list of 3 test scores. Change second to 100. Print list.

scores = [70, 80, 90]
scores[1] = 100
print(scores)

200

Read and print each line from data.txt.

with open("data.txt", "r") as f:
  for line in f:
    print(line.strip())

200

Ask for a word. Show first char, last char, and length.

word = input("Enter a word: ")
print("First:", word[0], "Last:", word[-1], "Length:", len(word))

200

Remove punctuation from a sentence.

import string
text = input("Enter a sentence: ")
for p in string.punctuation:
  text = text.replace(p, "")
print(text)

300

Write calculate_total(price, tax_rate) that returns total after tax.

def calculate_total(price, tax_rate):
  return price + (price * tax_rate)
p = float(input("Price: "))
t = float(input("Tax rate (e.g. 0.07): "))
print("Total:", calculate_total(p, t))

300

Ask for 5 integers, store in list, print sum and average.

nums = []
for i in range(5):
  nums.append(int(input("Enter number: ")))
print("Sum:", sum(nums))
print("Average:", sum(nums)/len(nums))

300

Read values from sales.txt, compute total.

total = 0
with open("sales.txt") as f:
  for line in f:
    total += float(line.strip())
print("Total:", total)

300

Ask for sentence. Print each character on its own line.

sentence = input("Enter a sentence: ")
for char in sentence:
  print(char)

300

Write get_positive_integer() that loops until a valid positive number is entered.

def get_positive_integer():
  while True:
    n = int(input("Enter positive integer: "))
    if n > 0:
      return n
count = get_positive_integer()
for i in range(count, 0, -1):
  print(i)

400

Write get_user_input() to return a name, and display_welcome(name) to print it.

def get_user_input():
  return input("Enter your name: ")
def display_welcome(name):
  print(f"Welcome, {name}!")
name = get_user_input()
display_welcome(name)

400

Ask for 7 numbers. Print smallest and largest.

nums = [int(input("Enter number: ")) for _ in range(7)]
print("Min:", min(nums))
print("Max:", max(nums))

400

Count lines in poem.txt.

with open("poem.txt") as f:
  lines = f.readlines()
print("Line count:", len(lines))

400

Count how many times “e” appears in a sentence.

text = input("Enter a sentence: ")
print("Count of e:", text.count('e'))

400

Read numbers from input.txt, double them, write to output.txt.

with open("input.txt") as infile, open("output.txt", "w") as outfile:
  for line in infile:
    num = float(line.strip())
    outfile.write(str(num * 2) + "\n")

500

Write analyze_number(num) to return square, cube, and square root. Print all three.

import math
def analyze_number(num):
  return num2, num3, math.sqrt(num)
n = float(input("Enter number: "))
sqr, cube, root = analyze_number(n)
print("Square:", sqr, "Cube:", cube, "Root:", root)

500

Remove all 0s from list, then sort.

nums = [0, 2, 0, 4, 5, 0, 3]
nums = [n for n in nums if n != 0]
nums.sort()
print(nums)

500

Read scores from scores.txt, find highest.

scores = [float(line.strip()) for line in open("scores.txt")]
print("Highest:", max(scores))

500

Print string in reverse with slicing. Show middle character too.

text = input("Enter text: ")
print("Reverse:", text[::-1])
print("Middle:", text[len(text)//2])

500

A VERY HARD QUESTION

just kidding