Py Set 4
Py Set 4
**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.
if cm < 0:
print("Invalid length")
else:
inches = cm / 2.54
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.
if unit.lower() == "celsius":
**Enroll:** **Subject:**
3. Ask the user to enter a temperature in Celsius. The program should print a
message based on the temperature.
elif temp == 0:
else:
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:**
print("Student is a freshman")
print("Student is a sophomore")
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
if guess == num:
else:
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.
cost_per_item = 12
cost_per_item = 10
else:
cost_per_item = 7
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.
print("Close")
else:
print("Not close")
**Name:** *Class:**
**Enroll:** **Subject:**
else:
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.]
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)
if ans == x * y:
print("Correct")
else:
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.
if ampm == 1:
else:
elif ampm == 2:
**Enroll:** **Subject:**
else:
else:
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():
computer_score = 0
**Name:** *Class:**
**Enroll:** **Subject:**
user_score = 0
for i in range(5):
print(f"\nRound {i+1}:")
computer_choice = random.choice(choices)
if user_choice == computer_choice:
if computer_choice == "Scissors":
user_score += 1
else:
computer_score += 1
if computer_choice == "Rock":
user_score += 1
else:
**Enroll:** **Subject:**
computer_score += 1
if computer_choice == "Paper":
user_score += 1
else:
computer_score += 1
else:
if __name__ == "__main__":
game()