Homework hacks
- Find the median test greade of a class
- create a simple game using random numbers
scores = [89,78,69,93,98,100,97,85,89,90]
scores.sort()
n = len(scores)
if n % 2 == 1:
median = scores[n//2]
else:
mid1 = scores[n//2-1]
mid2 = scores[n//2]
median = (mid1 + mid2)/2
print(f"the median test score is:, {median}")
the median test score is:, 89.5
game using random numbers
import random
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
# Number of attempts
attempts = 0
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
while True:
try:
# Get the player's guess
guess = int(input("Enter your guess: "))
attempts += 1
# Check if the guess is correct
if guess == secret_number:
print(f"Congratulations! You've guessed the number {secret_number} in {attempts} attempts.")
break
elif guess < secret_number:
print("Try a higher number.")
else:
print("Try a lower number.")
except ValueError:
print("Please enter a valid number.")
# End of the game
Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.
Try a higher number.
Try a higher number.
Try a higher number.
Try a higher number.
Try a lower number.
Try a lower number.
Try a lower number.
Try a lower number.
Try a lower number.
Congratulations! You've guessed the number 91 in 10 attempts.