KEMBAR78
Manu Py Set 4 | PDF | Computing | Computer Engineering
0% found this document useful (0 votes)
21 views16 pages

Manu Py Set 4

The document contains a series of Python programming exercises aimed at various tasks such as converting measurements, temperature conversions, identifying student classifications based on credits, and implementing games like Rock-Paper-Scissors. Each exercise includes an aim, a program code, and expected output. The exercises are designed to enhance programming skills through practical applications.

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)
21 views16 pages

Manu Py Set 4

The document contains a series of Python programming exercises aimed at various tasks such as converting measurements, temperature conversions, identifying student classifications based on credits, and implementing games like Rock-Paper-Scissors. Each exercise includes an aim, a program code, and expected output. The exercises are designed to enhance programming skills through practical applications.

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/ 16

E.

No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

Practical set=4

1 AIM : 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. centimeters
in an inch
PROGRAM :
len = int(input("Enter length in cm: ")) if

len < 0:

print("Invalid input") else:

inch = len / 2.54 print(len, "centimetres is equal

to", inch, "inches")

OUTPUT :

2 AIM : Ask the user for a temperature. Then ask them what
units, Celsius or Fahrenheit, the temperature is in. Your

1
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

program should convert the temperature to the other


unit. The conversions are F=9/5 C+32 and C=5/9(F-32).
PROGRAM :
temp = float(input("Enter Temperature: ")) unit =

input("Enter unit('C' for Celsius or 'F' for Fahrenheit): ") if unit

== 'C' or unit == 'c' :

newTemp = 9 / 5 * temp + 32

print("Temperature in Fahrenheit =", newTemp)

elif unit == 'F' or unit == 'f' :

newTemp = 5 / 9 * (temp - 32)

print("Temperature in Celsius =", newTemp)

else :

print("Unknown unit", unit)

OUTPUT :

3 AIM : Ask the user to enter a temperature in Celsius. The


program should print a message based on the
temperature:
• If the temperature is less than -273.15, print that the temperature is
invalid because it is below absolute zero.

2
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

• If it is exactly -273.15, print that the temperature is absolute 0. • If the


temperature is between -273.15 and 0, print that the temperature is
below freezing.
• If it is 0, print that the temperature is at the freezing point.
• If it is between 0 and 100, print that the temperature is in the normal
range.
• If it is 100, print that the temperature is at the boiling point.
• If it is above 100, print that the temperature is above the boiling point.

PROGRAM :
def temperature_message(celsius):

if celsius < -273.15:

print("The temperature is invalid because it is below absolute zero.")

elif celsius == -273.15:

print("The temperature is absolute 0.")

elif -273.15 < celsius < 0:

print("The temperature is below freezing.")

elif celsius == 0:

print("The temperature is at the freezing point.")

elif 0 < celsius < 100:

print("The temperature is in the normal range.")

elif celsius == 100:

print("The temperature is at the boiling point.")

elif celsius > 100:

3
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

print("The temperature is above the boiling point.")


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

OUTPUT :

4 AIM : 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.
PROGRAM :
credits = int(input("How many credits have you taken? ")) if

credits <= 23:

print("You are a freshman.") elif

24 <= credits <= 53:

4
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

print("You are a sophomore.") elif

54 <= credits <= 83:

print("You are a junior.") else:

print("You are a senior.")

OUTPUT :

5 AIM : 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.
PROGRAM :
import random number = random.randint(1,

10) r_number=int(input("Guess the number

: ")) if r_number == number:

print("Congratulations! You guessed it right!") else:

print("You guessed it wrong!")

OUTPUT :

5
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

6 AIM : 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 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.
PROGRAM :
def calculate_total_cost(num_items):

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 return

total_cost def main():

num_items = int(input("Enter the number of items you are buying: "))

if num_items < 0:

print("The number of items cannot be negative.")

return

total_cost = calculate_total_cost(num_items)

print(f"Total cost = ${total_cost:.2f}.") if

__name__ == "__main__":

main()

6
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

OUTPUT :

7 AIM : 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.
PROGRAM :
a = float(input("Enter first number: ")) b

= float(input("Enter second number: ")) d

= 0 if a > b : d = a - b else :

d = b - a if d

<= 0.001 :

print("Close")

else :

print("Not Close")

OUTPUT :

8 AIM : 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.

7
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

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

(year % 400 == 0):

print(f"{year} is a leap year.") else:

print(f"{year} is not a leap year.")

OUTPUT :

9 AIM : Write a program that asks the user to enter a number


and prints out all the divisors of that number.
PROGRAM :
number = int(input("Enter a number: ")) number =

abs(number) divisors = [i for i in range(1, number + 1) if

number % i == 0] print(f"Divisors of {number} are:

{divisors}")

OUTPUT :

8
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

10 AIM : 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.
PROGRAM :
import random print("Welcome to the

multiplication game") num_questions = 10

score = 0 for i in range(num_questions):

num1 = random.randint(1, 10) num2 = random.randint(1,

10) user_answer = int(input(f"Question {i + 1}: {num1} x

{num2}? ")) correct_answer = num1 * num2 if user_answer

== correct_answer:

9
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

print("Right!")

score += 1 else:

print(f"Wrong! The correct answer is {correct_answer}.")

print(f"Your score is {score} out of 10")

OUTPUT :

10
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

11 AIM : 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.
PROGRAM :
def calculate_future_time(hour, period, hours_in_future):

if period == 'pm' and hour != 12:

hour += 12 elif period == 'am'

and hour == 12:

hour = 0

future_hour = (hour + hours_in_future) % 24

future_period = 'am' if future_hour >= 12:

future_period = 'pm' if future_hour > 12:

future_hour -= 12 elif future_hour == 0:

future_hour = 12

return future_hour, future_period def

main():

hour = int(input("Enter hour: "))

if hour < 1 or hour > 12:

print("Hour must be between 1 and 12.")

11
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

return
period_choice = int(input("am (1) or pm (2)? "))

if period_choice == 1: period = 'am'

elif period_choice == 2:

period = 'pm'

else:

print("Please enter 1 for AM or 2 for PM.")

return

hours_in_future = int(input("How many hours ahead? "))

if hours_in_future < 0:

print("Number of hours ahead must be non-negative.")

return

future_hour, future_period = calculate_future_time(hour, period, hours_in_future)

print(f"New hour: {future_hour} {future_period}") main()

OUTPUT :

12 AIM : 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:

12
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

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 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.
PROGRAM :
x=200

for i in range(x):

if i % 5 == 2:

if i % 6 == 3:

if i % 7 == 2:

print(i, 'candies are in the bowl.')

OUTPUT :

13 AIM : 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.

13
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

PROGRAM :
import random def

get_computer_choice():

"""Returns a random choice for the computer."""

choices = ['rock', 'paper', 'scissors'] return

random.choice(choices) def

determine_winner(user_choice, computer_choice):

"""Determines the winner of a single round.""" if

user_choice == computer_choice:

return 'tie'

elif (user_choice == 'rock' and computer_choice == 'scissors') or \

(user_choice == 'paper' and computer_choice == 'rock') or \

(user_choice == 'scissors' and computer_choice == 'paper'):

return 'user'

else:

return 'computer' def

main():

user_score = 0

computer_score = 0 rounds

= 5 print("Welcome to Rock-

Paper-Scissors!") for

round_number in range(1,

rounds + 1):

user_choice = input(f"Round {round_number} - Enter rock, paper, or scissors: ").strip().lower()

while user_choice not in ['rock', 'paper', 'scissors']:

print("Invalid choice. Please enter rock, paper, or scissors.") user_choice =

input(f"Round {round_number} - Enter rock, paper, or scissors: ").strip().lower()

computer_choice = get_computer_choice() print(f"Computer chose: {computer_choice}")

result = determine_winner(user_choice, computer_choice)

14
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

if result == 'user': print("You

win this round!") user_score += 1

elif result == 'computer':

print("Computer wins this round!")

computer_score += 1

else:

print("It's a tie for this round!") print(f"Score - You:

{user_score}, Computer: {computer_score}\n") if user_score >

computer_score:

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

elif computer_score > user_score:

print("Sorry! The computer won the game.")

else:

print("The game is a tie!") main()

OUTPUT :

15
E.No – 22171341203 Emmanuel Zurumba IOT –CE- C

Python

16

You might also like