0% found this document useful (0 votes)
22 views9 pages

Python Projects: Games & Utilities

The document outlines several Python programming exercises including a number guessing game, a simple calculator, a to-do list, a rock-paper-scissors game, a password generator, a quiz game, and a currency converter. Each section provides code snippets and explanations for functionality, user input handling, and error checking. The exercises demonstrate various programming concepts such as loops, conditionals, and data structures.

Uploaded by

alswell3208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

Python Projects: Games & Utilities

The document outlines several Python programming exercises including a number guessing game, a simple calculator, a to-do list, a rock-paper-scissors game, a password generator, a quiz game, and a currency converter. Each section provides code snippets and explanations for functionality, user input handling, and error checking. The exercises demonstrate various programming concepts such as loops, conditionals, and data structures.

Uploaded by

alswell3208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

GUESS THE NUMBER GAME


Q1.

import random

number = [Link](1, 100)

print(f"Random number is: {number}")

Q2.

hidden_number = [Link](1, 100)


guess = int(input("Enter your guess: "))

while guess != hidden_number:


guess = int(input("Wrong! Try again: "))

print("Wow! You guessed the number.")

Q3.

if guess < hidden_number:


print("Too low!")
elif guess > hidden_number:
print("Too high!")
else:
print("You guessed correctly!")

Q4.

choice = input("Type 'easy' or 'hard' (must use all lowercase!): ")

if choice == "easy":

max_range = 50

max_attempts = 8

elif choice == "hard":

max_range = 100

max_attempts = 5
else:

print("Invalid choice. Setting to default (medium level).")

max_range = 75

max_attempts = 6

2. SIMPLE CALCULATOR

Q1.

def calc(num1, num2, operator):


if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '/':
return num1 / num2
elif operator == '*':
return num1 * num2
else:
print("Invalid operator")

Q2.

Using float() allows the program to handle both decimal numbers and whole numbers,

Using int() will limit the program to whole number only which would cause an error when
the user enters a decimal number.

Q3.

if operator == '/' and num2 == 0:

print("Error: Division by zero is not allowed")

else:

result = calc(num1, num2, operator)


Q4.

If the user enters an operator that is not recognized, the program will go to the else part of
the function and return an error message.

else:

return "Invalid operator"

3. To-Do List (Console)


Q1.
tasks = []
task = input("Enter a task: ")
[Link](task)
Q2.
The enumerate() function helps you loop through a list and automatically get both
the index number of each task and the task itself.
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
Q3.
index = int(input("Enter the task number to remove: ")) - 1

if 0 <= index < len(tasks):


removed_task = [Link](index)
print(f"{removed_task} is removed")
else:
print("Invalid task number!")
Q4.
with open("[Link]", "w") as file:
for task in tasks:
[Link](task + "\n")

with open("[Link]", "r") as file:


tasks = [[Link]() for line in [Link]()]

4. ROCK, PAPER SCISSORS


Q1.

import random
choices = ["rock", "paper", "scissors"]
computer = [Link](choices)

Q2.

if (user == "rock" and computer == "scissors") or \

(user == "paper" and computer == "rock") or \

(user == "scissors" and computer == "paper"):

print("You win!")

Q3.

The while True loop keeps the game running continuously until the player decides to stop.
Inside the loop, we can ask after each round if the player wants to continue
while True:

again = input("Play again? (yes/no): ")

if [Link]() != "yes":

break

Q4.

player_score = 0

computer_score = 0

if user_wins:

player_score += 1

elif computer_wins:

computer_score += 1

print(f"Score: You = {player_score} points & Computer = {computer_score} points")

5. PASSWORD GENERATOR

Q1.

import string

characters = string.ascii_letters + [Link] + [Link]

Q2.

[[Link](characters) for _ in range(length)]

Q3.

Using a mix of letters, numbers, and symbols makes your password much stronger and
harder for anyone to guess.
Q4.

import string

import random

use_symbols = input("Do you want to include special characters (yes/no): ")

characters = string.ascii_letters + [Link]

if use_symbols.lower() == "yes":

characters += [Link]

password = ''.join([Link](characters) for _ in range(12))

print("Your password is:", password)

6. QUIZ GAME
Q1.

question = {

"What is the capital of Ghana?": "Accra",

"What is 2 + 1?": "8"

}
Q2.

It changes all letters in a string to lowercase, this makes comparisons case-sensitive.

It prevents errors caused by lower and upper case letters.

answer = input("Your answer: ")

if [Link]() == "Accra":

print("Correct!")

Q3.

score = 0

if [Link]() == correct_answer:

score += 1

Q4.

quest = {

"Where is GCTU located?": {

"options": ["A. Lapaz", "B. Tesano", "C. Kwashieman"],

"answer": "b"

for question in quest:

print(question)

for option in quest[question]["options"]:

print(option)

user_answer = input("Choose A, B, or C: ").lower()

if user_answer == quest[question]["answer"]:
print("Correct!")

else:

print("Wrong!")

7. CURRENCY CONVERTER
Q1.

rates = {
"USD": 12.0,
"EUR": 13.0,
"JPY": 14.0
}
print(rates)

Q2.

:.2f is important because it formats a number to 2 decimal places, which is standard for
currencies.

amount = 450.543

print(f"You have {amount:.2f} cedis")

Q3.

.upper() converts all letters to upper case so, it ensures that the currency code is case
sensitive.

currency = input("Enter currency code: ").upper()

Q4.

import requests

response = [Link]("[Link]

data = [Link]()

usd_rate = data["rates"]["USD"]
print("The current exchange rate for USD is:", round(usd_rate, 2), "GHS")

You might also like