Chaudhari Armin Bharartbhai 22171371011
Practical-5
Aim :- If statement
#print("practical --5.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.
from random import randint
length = eval(input("Enter length in CM: "))
if length< 0:
print("not valid")
else:
length = length / 2.54
print(length," inches")
#print("practical--5.2")
#Ask the user for a temperature. Then ask them what units, Celsius or Fahrenheit, the temper-
#ature is in. Your program should convert the temperature to the other unit. The conversions
#are F =9 /5C + 32 and C =5/9(F − 32).
temp = float(input("Enter Temperature: "))
unit = input("Enter unit('C' for Celsius or 'F' for Fahrenheit): ")
if unit == 'C' or unit == 'c' :
Temp = 9 / 5 * temp + 32
print("Temperature in Fahrenheit =",Temp)
elif unit == 'F' or unit == 'f' :
Temp = 5 / 9 * (temp - 32)
print("Temperature in Celsius =",Temp)
else :
print("Unknown unit", unit)
#print("practical--5.3")
#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.
#• 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.
44
Chaudhari Armin Bharartbhai 22171371011
#• 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.
celcuis = eval(input("Enter temperature in Celcuis: "))
zero = -273.15
if celcuis < zero:
print("Temperature is invalid and below freezing point")
elif celcuis == zero:
print("The temperature is absolute zero")
elif celcuis <= 0 :
print("Temperature is below freezing point")
elif celcuis == 0:
print("Temperatur is at freezing point")
elif celcuis > 0 and celcuis < 100:
print("Temperature is in normal range")
elif celcuis == 100:
print("The temperature is at boiling point")
elif celcuis > 100:
print("The temperature is above boiling point")
#print("practical--5.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.
credits = eval(input("Enter the number of credit taken: "))
if credits<= 23:
print("You are a fresh man")
elif credits >= 24 and credits <= 53:
print("You are a sophormore")
elif credits >= 54 and credits <= 83:
print("You are a Junior")
elif credits >= 84:
print("Congrats You are a Senior")
#print("practical--5.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.
from random import randint
random_number = randint(1,10)
45
Chaudhari Armin Bharartbhai 22171371011
game = 0
while game < 6:
guess = eval(input("Guess generated random number: "))
game =game + 1
if guess == random_number:
print("You Guessed right!")
break
elif guess > random_number:
print("Input number is greater than Random number")
#print("practical--5.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 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.
item = eval(input("How many items do you want to buy:"))
if item < 10:
print("Total price is: ", item*10)
elif item in range(10,100):
print("Total price is:", item*10)
elif item >= 100:
print("Total price is: ",item*7 )
#print("practical--5.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 = eval(input("Enter First Number: "))
num2 = eval(input("Enter Second Number:"))
if num1 + 0.001 == num2:
print("close")
elif num2 + 0.001 == num1:
print("close")
else:
print("Not close")
#print("practical--5.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 Year To Check if its a leap year: "))
if year % 4 == 0 or year % 100 != 0 and year % 400 == 0:
46
Chaudhari Armin Bharartbhai 22171371011
print(year," Is a leap year")
else:
print(year,"Is not a leap Year")
#print("practical--5.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 Section3.2.]
number = eval(input("Enter a number: "))
for i in range(1,number):
if number % i == 0:
print( i)
#print("practical--5.10")
#Write a multiplication game program for kids. The program should give the player ten ran-
#domly 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.
#Question 1: 3 x 4 = 12
#Right!
#Question 2: 8 x 6 = 44
#Wrong. The answer is 48.
#...
#...
#Question 10: 7 x 7 = 49
#Right.
from random import randint
questions = 0
for questions in range(10):
num1 = randint(1, 10)
num2 = randint(1, 10)
questions += 1
print("Question ",questions,":" ,num1 ,"X",num2)
solution =eval(input("Enter Solution: "))
if(solution == num1 * num2):
print("YaY you got it Right!!")
else:
print(solution,"Your Answer is Wrong")
#print("practical--5.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. An example is shown
#below.
47
Chaudhari Armin Bharartbhai 22171371011
#Enter hour: 8
#am (1) or pm (2)? 1
#How many hours ahead? 5
#New hour: 1 pm
hour = eval(input("Enter Hour Between 1 and 12: "))
ampm = input("Choose 1 for AM and 2 for pm")
future = eval(input("Enter Hours in the future: "))
if ampm == 1:
print("Future Hours ", ampm + future)
#print("practical--5.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 candies in range(200):
if (candies % 5 != 2):
continue
if (candies % 6 != 3):
continue
if (candies % 7 != 2):
continue
print(str(candies) + " is the answer!")
break
#print("practical--5.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.
from random import randint
game = 0
rock = 1
paper =2
scissors =3
wins =0
loose =0
48
Chaudhari Armin Bharartbhai 22171371011
for game in range(5):
print("Game",game,"Start")
my_hand = eval(input("Choose Rock , paper or scissors: "))
computer_hand = randint(1, 3)
if my_hand == computer_hand :
print ("You and Computer Played the same hand its a tie..")
elif my_hand == rock and computer_hand == scissors:
print("Yay I crushed the computer scissors")
wins += 1
elif my_hand == rock and computer_hand == paper:
print("Oops Computer Cover my Rock")
loose += 1
elif my_hand == paper and computer_hand == rock:
print("Yay!! I covered computer rock haha")
wins += 1
elif my_hand == paper and computer_hand == scissors:
print("Oops Computer cut me up into piece")
loose += 1
elif my_hand == scissors and computer_hand == rock:
print("Oops Computer Shattered My scissors")
loose += 1
elif my_hand == scissors and computer_hand == paper:
print("Yay I cut up computer paper into pieces")
wins += 1
if wins > loose:
print("You win ", "You Scored ",wins,"Computer Scored",loose)
else:
print("You loosed Try Again")
Output:
Enter length in CM: 5
1.968503937007874 inches
Enter Temperature: 36
Enter unit('C' for Celsius or 'F' for Fahrenheit): c
49
Chaudhari Armin Bharartbhai 22171371011
Temperature in Fahrenheit = 96.8
Enter temperature in Celcuis: 36
Temperature is in normal range
Enter the number of credit taken: 5
You are a fresh man
Guess generated random number: 25
Input number is greater than Random number
Guess generated random number: 2
Guess generated random number: 5
Guess generated random number: 5
Guess generated random number: 5
Guess generated random number: 5
How many items do you want to buy:5
Total price is: 50
Enter First Number: 4
Enter Second Number:30
Not close
Enter Year To Check if its a leap year: 2024
2024 Is a leap year
Enter a number: 5
1
Question 1 : 2 X 3
Enter Solution: 6
YaY you got it Right!!
Question 2 : 1 X 9
Enter Solution: 9
YaY you got it Right!!
Question 3 : 5 X 2
Enter Solution: 10
YaY you got it Right!!
Question 4 : 6 X 1
Enter Solution: 6
YaY you got it Right!!
Question 5 : 9 X 9
Enter Solution: 81
YaY you got it Right!!
50
Chaudhari Armin Bharartbhai 22171371011
Question 6 : 2 X 6
Enter Solution: 12
YaY you got it Right!!
Question 7 : 3 X 8
Enter Solution: 18
18 Your Answer is Wrong
Question 8 : 4 X 3
Enter Solution: 12
YaY you got it Right!!
Question 9 : 3 X 7
Enter Solution: 21
YaY you got it Right!!
Question 10 : 4 X 9
Enter Solution: 36
YaY you got it Right!!
Enter Hour Between 1 and 12: 5
Choose 1 for AM and 2 for pm2
Enter Hours in the future: 53
177 is the answer!
Game 0 Start
Choose Rock , paper or scissors: rock
Oops Computer Cover my Rock
Game 1 Start
Choose Rock , paper or scissors: 2
You and Computer Played the same hand its a tie..
Game 2 Start
Choose Rock , paper or scissors: 3
Yay I cut up computer paper into pieces
Game 3 Start
Choose Rock , paper or scissors: 1
Oops Computer Cover my Rock
Game 4 Start
Choose Rock , paper or scissors: 2
Yay!! I covered computer rock haha
You loosed Try Again
51