Example using the Random library

%%js
let num1 = Math.floor(Math.random() * 21); // Generates a random integer between 0 and 20
let num2 = Math.floor(Math.random() * 21); // Generates a random integer between 0 and 20

if (num1 > num2) {
    console.log(num1 + ' > ' + num2);
} else if (num1 < num2) {
    console.log(num1 + ' < ' + num2);
} else {
    console.log(num1 + ' = ' + num2);
}
<IPython.core.display.Javascript object>
from random import randint
num_1 = randint(0,20)
num_2 = randint(0,20)
if num_1>num_2:
    print(num_1,'>',num_2)
elif num_1<num_2:
    print(num_1,'<',num_2)
else:
    print(num_1,'=',num_2)
16 > 3
  • This code imports the randint function from the random module.
  • Two variables, num_1 and num_2, are assigned random integers between 0 and 20 using random.randint(0, 20).
  • A conditional checks if num_1 is greater than num_2.
  • If num_1 is greater than num_2, it prints num_1 > num_2.
  • If the first condition is False, the next condition checks if num_1 is less than num_2.
  • If num_1 is less than num_2, it prints num_1 < num_2.
  • If neither condition is True (the numbers are equal), the conditional moves to the else statement.
  • The else statement prints num_1 = num_2.

Example using Boolean and Random

import random

random_num = random.randint(-10, 10)  # Generates a random integer between -10 and 10

# Check if the number is even or odd
if random_num % 2 == 0:
    print(f"{random_num} is even.")
else:
    print(f"{random_num} is odd.")
1 is odd.
  • Random Number Generation: The code generates a random integer between -10 and 10 using random.randint().
  • Even or Odd Check: It checks if the generated number is even or odd by using the modulo operator %, which determines if there is a remainder when dividing by 2.
  • Output Result: It prints a message indicating whether the random number is even or odd based on the result of the conditional check.

Homework

Directions:

  • Make a quiz game on any topic of you choice
  • Have 5 questions based on you topic
  • Use a function to ask the question
  • Use modules if you would like(Not requried)
  • Use the language of your choice
import sys

def question_with_response(prompt):
    answer = input(prompt)
    return answer

questions = 5
correct = 0

user_name = question_with_response("Enter your name: ")
print(f'Hello, {user_name}! You are running Python using {sys.executable}.')
ready = question_with_response("Are you ready to take a sports quiz? (yes/no): ")
if ready.lower() != 'yes':
    print("Come back when you're ready! Goodbye.")
    sys.exit()

q1 = question_with_response("1. How many players are on the field for one team in American football? (a) 11 (b) 9 (c) 15: ")
if q1.lower() == "a":
    print("Correct!")
    correct += 1
else:
    print("Incorrect. The correct answer is (a) 11.")

q2 = question_with_response("2. In basketball, how many points is a shot from beyond the three-point line worth? (a) 2 (b) 3 (c) 4: ")
if q2.lower() == "b":
    print("Correct!")
    correct += 1
else:
    print("Incorrect. The correct answer is (b) 3.")

q3 = question_with_response("3. What is the maximum duration of a standard professional soccer match (without extra time)? (a) 60 minutes (b) 90 minutes (c) 120 minutes: ")
if q3.lower() == "b":
    print("Correct!")
    correct += 1
else:
    print("Incorrect. The correct answer is (b) 90 minutes.")

q4 = question_with_response("4. In tennis, what is the term used when the score is 40-40? (a) Deuce (b) Match point (c) Advantage: ")
if q4.lower() == "a":
    print("Correct!")
    correct += 1
else:
    print("Incorrect. The correct answer is (a) Deuce.")

q5 = question_with_response("5. In which year did the first modern Olympic Games take place? (a) 1896 (b) 1900 (c) 1912: ")
if q5.lower() == "a":
    print("Correct!")
    correct += 1
else:
    print("Incorrect. The correct answer is (a) 1896.")

print(f'{user_name}, you scored {correct}/{questions}!')

print("\nHomework Hack: Let's try a simple comparison program!")

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

if num1 == num2:
    print("Both numbers are equal.")
elif num1 > num2:
    print(f"The first number ({num1}) is larger.")
else:
    print(f"The second number ({num2}) is larger.")
  • The program starts by importing the sys module and defining a function question_with_response to handle user input.
  • The quiz consists of 5 multiple-choice questions, each checking the user’s answer and increasing a correct counter for every right response.
  • Personalized interaction is achieved by greeting the user with their name and confirming if they are ready to proceed with the quiz.
  • After the quiz, the program displays the user’s total score based on the number of correct answers out of 5.
  • The code includes a “homework hack” where the user inputs two numbers, and the program compares and prints whether they are equal or which one is larger.