Practical-3
Practical-3
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.
#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).
#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.
#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.
#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.
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.
#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.
46
Chaudhari Armin Bharartbhai 22171371011
#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.]
#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.
#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.
rock = 1
paper =2
scissors =3
wins =0
loose =0
48
Chaudhari Armin Bharartbhai 22171371011
else:
print("You loosed Try Again")
Output:
Enter Temperature: 36
Enter unit('C' for Celsius or 'F' for Fahrenheit): c
49
Chaudhari Armin Bharartbhai 22171371011
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!!
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