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

Py Set 4

The document contains a series of programming exercises designed to practice basic programming concepts such as user input, conditionals, loops, and functions. Each exercise includes a specific task, such as converting measurements, determining temperature ranges, or playing games like Rock-Paper-Scissors. The exercises aim to enhance problem-solving skills and programming proficiency.

Uploaded by

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

Py Set 4

The document contains a series of programming exercises designed to practice basic programming concepts such as user input, conditionals, loops, and functions. Each exercise includes a specific task, such as converting measurements, determining temperature ranges, or playing games like Rock-Paper-Scissors. The exercises aim to enhance problem-solving skills and programming proficiency.

Uploaded by

ejoshua033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

**Name:** *Class:**

**Enroll:** **Subject:**

# Practical Set 4

1. Write a program that asks the user to enter a length in centimeters. If the
user enters a negative length, the program should tell the user that the entry
is invalid. Otherwise, the program should convert the length to inches and
print out the result. There are 2.54 centimeters in an inch.

cm = float(input("Enter a length in centimeters: "))

if cm < 0:

print("Invalid length")

else:

inches = cm / 2.54

print(f"{cm} cm is equal to {inches} inches")

2. Ask the user for a temperature. Then ask them what units, Celsius or
Fahrenheit, the temperature is in. Your program should convert the
temperature to the other unit.

temp = float(input("Enter a temperature: "))

unit = input("Is the temperature in Celsius or Fahrenheit? ")

if unit.lower() == "celsius":

fahrenheit = (temp * 9/5) + 32

print(f"{temp}°C is equal to {fahrenheit}°F")

elif unit.lower() == "fahrenheit":

celsius = (temp - 32) * 5/9


**Name:** *Class:**

**Enroll:** **Subject:**

print(f"{temp}°F is equal to {celsius}°C")

3. Ask the user to enter a temperature in Celsius. The program should print a
message based on the temperature.

temp = float(input("Enter a temperature in Celsius: "))

if temp < -273.15:

print("Invalid temperature: below absolute zero")

elif temp == -273.15:

print("Temperature is absolute 0")

elif -273.15 < temp < 0:

print("Temperature is below freezing")

elif temp == 0:

print("Temperature is at the freezing point")

elif 0 < temp < 100:

print("Temperature is in the normal range")

elif temp == 100:

print("Temperature is at the boiling point")

else:

print("Temperature is above the boiling point")

4. Write a program that asks the user how many credits they have taken. If
they have taken 23 or less, print that the student is a freshman. If they have
taken between 24 and 53, print that they are a sophomore. The range for
juniors is 54 to 83, and for seniors it is 84 and over.
**Name:** *Class:**

**Enroll:** **Subject:**

credits = int(input("Enter the number of credits taken: "))

if credits <= 23:

print("Student is a freshman")

elif 24 <= credits <= 53:

print("Student is a sophomore")

elif 54 <= credits <= 83:

print("Student is a junior")

else:

print("Student is a senior")

5. Generate a random number between 1 and 10. Ask the user to guess the
number and print a message based on whether they get it right or not.

import random

num = random.randint(1, 10)

guess = int(input("Guess a number between 1 and 10: "))

if guess == num:

print("You got it right!")

else:

print(f"Sorry, the number was {num}. Try again!")

6. A store charges $12 per item if you buy less than 10 items. If you buy
between 10 and 99 items, the cost is $10 per item. If you buy 100 or more
**Name:** *Class:**

**Enroll:** **Subject:**

items, the cost is $7 per item. Write a program that asks the user how many
items they are buying and prints the total cost.

num_items = int(input("How many items are you buying? "))

if num_items < 10:

cost_per_item = 12

elif 10 <= num_items < 100:

cost_per_item = 10

else:

cost_per_item = 7

total_cost = num_items * cost_per_item

print("The total cost is $", total_cost)

7. Write a program that asks the user for two numbers and prints Close if the
numbers are within .001 of each other and Not close otherwise.

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

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

if abs(num1 - num2) < 0.001:

print("Close")

else:

print("Not close")
**Name:** *Class:**

**Enroll:** **Subject:**

8. A year is a leap year if it is divisible by 4, except that years divisible by


100 are not leap years unless they are also divisible by 400. Write a program
that asks the user for a year and prints out whether it is a leap year or not.

year = int(input("Enter a year: "))

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):

print(year, "is a leap year")

else:

print(year, "is not a leap year")

9. Write a program that asks the user to enter a number and prints out all
the divisors of that number. [Hint: the % operator is used to tell if a number
is divisible by something. See Section 3.2.]

num = int(input("Enter a number: "))

print("The divisors of", num, "are:")

for i in range(1, num + 1):

if num % i == 0:

print(i)

10. Write a multiplication game program for kids. The program should give
the player ten randomly generated multiplication questions to do. After each,
the program should tell them whether they got it right or wrong and what
the correct answer is.

import random
**Name:** *Class:**

**Enroll:** **Subject:**

for i in range(10):

x = random.randint(1,10)

y = random.randint(1,10)

ans = int(input(f"Question : {x} * {y}="))

if ans == x * y:

print("Correct")

else:

print(f"Wrong, correct answer {x * y}")

11. Write a program that asks the user for an hour between 1 and 12, asks
them to enter am or pm, and asks them how many hours into the future they
want to go. Print out what the hour will be that many hours into the future,
printing am or pm as appropriate.

hour = int(input("Enter hour between 1 and 12: "))

ampm = int(input("Enter am(1)/pm(2): "))

x = int(input("How many hours further you want to go: "))

if ampm == 1:

if hour + x <= 12:

print("The time is", hour + x, "am")

else:

print("The time is", hour + x - 12, "pm")

elif ampm == 2:

if hour + x <= 12:

print("The time is", hour + x, "pm")


**Name:** *Class:**

**Enroll:** **Subject:**

else:

print("The time is", hour + x - 12, "am")

else:

print("Invalid time zone")

12. A jar of Halloween candy contains an unknown amount of candy and if


you can guess exactly how much candy is in the bowl, then you win all the
candy. You ask the person in charge the following: If the candy is divided
evenly among 5 people, how many pieces would be left over? The answer is
2 pieces. You then ask about dividing the candy evenly among 6 people, and
the amount left over is 3 pieces. Finally, you ask about dividing the candy
evenly among 7 people, and the amount left over is 2 pieces. By looking at
the bowl, you can tell that there are less than 200 pieces. Write a program to
determine how many pieces are in the bowl.

for i in range(2, 200):

if i % 5 == 2 and i % 6 == 3 and i % 7 == 2:

print(i)

13. Write a program that lets the user play Rock-Paper-Scissors against the
computer. There should be five rounds, and after those five rounds, your
program should print out who won and lost or that there is a tie.

import random

def game():

choices = ["Rock", "Paper", "Scissors"]

computer_score = 0
**Name:** *Class:**

**Enroll:** **Subject:**

user_score = 0

for i in range(5):

print(f"\nRound {i+1}:")

user_choice = input("Enter your choice (Rock, Paper, Scissors):


").capitalize()

while user_choice not in choices:

user_choice = input("Invalid choice. Please enter Rock, Paper, or


Scissors: ").capitalize()

computer_choice = random.choice(choices)

print(f"\nComputer chose: {computer_choice}")

if user_choice == computer_choice:

print(f"Both players selected {user_choice}. It's a tie!")

elif user_choice == "Rock":

if computer_choice == "Scissors":

print("Rock smashes scissors! You win this round.")

user_score += 1

else:

print("Paper covers rock! You lose this round.")

computer_score += 1

elif user_choice == "Paper":

if computer_choice == "Rock":

print("Paper covers rock! You win this round.")

user_score += 1

else:

print("Scissors cuts paper! You lose this round.")


**Name:** *Class:**

**Enroll:** **Subject:**

computer_score += 1

elif user_choice == "Scissors":

if computer_choice == "Paper":

print("Scissors cuts paper! You win this round.")

user_score += 1

else:

print("Rock smashes scissors! You lose this round.")

computer_score += 1

print(f"\nFinal Score - You: {user_score}, Computer: {computer_score}")

if user_score > computer_score:

print("You win the game! Congratulations!")

elif user_score < computer_score:

print("You lose the game. Better luck next time!")

else:

print("It's a tie game!")

if __name__ == "__main__":

game()

You might also like