085-1263404

Maths Grinds
8 Jun 2023

## Higher Level

# Question 16(a)

from random import randint

def guess_game(max_guesses_allowed):
difficulty = input("Enter difficulty E(asy) or H(ard): ")
if difficulty == "H":
upper = 100
else:
upper = 5

secret_number = randint(1, upper)
guess_count = 0
user_guess = 0
guesses = set()

while (user_guess != secret_number) and guess_count < max_guesses_allowed:

user_guess = int(input("Enter your guess: "))
if user_guess in guesses:
guess_count += 1 #Increase guess_count by 1
if user_guess == secret_number:
print("Congratulations! You win!")
print("You took",guess_count,"guesses. ")
elif user_guess < secret_number:
print("Sorry! Your guess was too low")
elif user_guess > secret_number:
print("Sorry! Your guess was too high")

print("Welcome to the guessing game!")
max_guesses_allowed = int(input("Enter the maximum number of guesses allowed: "))
guess_game(max_guesses_allowed)


# Question 16(b)
import random # import random
from math import fabs as abs # import absolute from math

user_score = 0  # set score to 0
again = "Y" # Default to playing at least once
while again == "Y":
secret_number = random.randint(1,100) # secret random number
user_guess = int(input("Enter your guess: "))  # user's guess
difference = int(abs(user_guess - secret_number)) # calcultes difference
message = "Secret number is "+str(secret_number)+". You guessed "+str(user_guess)+". Difference is "+str(difference)+"."  # output message
print(message) # print it!

if difference == 0: # Jackpot
print("JACKPOT!!! You score 100 points")
user_score += 100
elif difference <= 20: # Close
print("You score 20 points")
user_score += 20
elif difference > 30: # Too far away
print("You lose 30 points")
user_score -= 30
again = input("Play again? (Y/N):")

quit() # It's always good to end with a quit

## Ordinary Level

# Question 16(a)
# Examination Number:
print("********************")
print("Times Table program")
print("********************")

number = int(input("Enter number: "))
if number < 0:
print("This program does not support negative numbers. ")
else:
print("Multiplications of ", number)
for i in range(13):
print(i,"x", number, "=", number*i)

# Question 16(b)
print("Welcome to Temperature Alert System") # Welcome message

temp = int(input("Enter temperature value in degrees Celsius: ")) # Asks user for the temperature

if temp < 20:
print("Too cold. Turn up heating.") # Display this message when temp is less than 20
elif temp >= 20 and temp <= 24:
print("Temperature is just right.") # this message is for between 20 and 24 inclusive
elif temp > 24:
print("Too warm. Turn down heating.") # anything more than 24 is this message

quit() # always good to quit

This site uses Akismet to reduce spam. Learn how your comment data is processed.