This page will serve as a go to page for lesson, activites and code
Get in TouchIt uses Almost All of the Topics We Studies in Python Including:
import random # This is the module we'll use def get_random_number(): """Generates a random number between 1 and 10.""" return random.randint(1, 10) def get_user_guess(): """Prompts the user for a guess and returns it as an integer.""" guess = input("Enter your guess (1-10): ") return int(guess) def play_game(): """Main game logic.""" number_to_guess = get_random_number() guess = None attempts = 0 guesses = [] print("Welcome to the Guess the Number Game!") print("Try to guess the number between 1 and 10.") while guess != number_to_guess: guess = get_user_guess() guesses.append(guess) attempts += 1 if guess < number_to_guess: print("Too low!") elif guess > number_to_guess: print("Too high!") else: print(f"Correct! The number was {number_to_guess}.") print(f"It took you {attempts} attempts.") print("\nYour guesses were:") for g in guesses: print(g) # Run the game play_game()
An algorithm is a recipe for solving a problem.
How would you find the largest number given a list of numbers?
def find_max(num_list): max_num = num_list[0] for num in num_list: if num > max_num: max_num = num return max_num
Key Points About Pseudocode
START SET max TO first number in the list FOR each number in the list IF number > max THEN SET max TO number ENDIF ENDFOR PRINT max END
There is no strict standard, but it is usually
START INPUT number IF number MOD 2 = 0 THEN PRINT "Even" ELSE PRINT "Odd" ENDIF END
START INPUT num1, num2 SET sum TO num1 + num2 PRINT sum END
START FOR i FROM 1 TO 5 PRINT i ENDFOR END
START INPUT list of numbers INPUT target number SET count TO 0 FOR each number IN list IF number = target THEN INCREMENT count BY 1 ENDIF ENDFOR PRINT count END