Assign3 (T&T)
Assign3 (T&T)
SOURCE CODE-
numcm=float(input("enter a number in centimeter"))
if numcm<0:
print("the entry is invalid")
else:
numinch=numcm*2.54
print("the value in inch is",numinch)
OUTPUT-
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. The
conversions are F = 9 5C +32 and C = 5 9(F−32).
SOURCE CODE-
temp=float(input("enter the temperature in fahrenheit or celcius"))
unit=input("enter the unit(C or F):").upper()
if unit == "C":
ftemp=(temp*9/5)+32
print("the temperature is converted to fahrenheit",ftemp)
elif unit == "F":
ctemp=(temp-32)*5/9
print("the temperature is converted to celcius",ctemp)
else:
print("invalid input")
OUTPUT-
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. • 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.
SOURCE CODE-
temp=float(input("enter the temperature"))
if temp<-273.15:
print("Invalid as it is below freezing")
elif temp==0:
print("temp is at freezing point")
elif temp>=273.15 and temp<=0:
print("below freezing")
elif temp>0 and temp<100:
print("normal range")
elif temp==100:
print("it is at boiling point")
else:
print("above boiling point")
OUTPUT-
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.
SOURCE CODE-
num=int(input("enter a number"))
if num<=23:
print("the student is a freshman")
elif num>=24 and num<=53:
print("the student is a sophomore")
elif num>53 and num<=83:
print("juniors")
else:
print("senior")
OUTPUT-
5)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.
SOURCE CODE-
item=int(input("enter the number of items"))
if item<10:
cost=item*12*83.16
print(cost)
elif item>10 and item<99:
cost=item*10*83.16
print(cost)
else:
cost=item*7*83.16
print(cost)
OUTPUT-
6)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.
SOURCE CODE-
num1=float(input("enter first number"))
num2=float(input("enter second number"))
if num1-num2==0.001:
print("close")
else:
print("not close")
OUTPUT-
SOURCE CODE-
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("not a leap year")
OUTPUT-
8)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.
SOURCE CODE-
user_input = input("Enter hour: ")
input_parts = user_input.split()
if len(input_parts) != 2:
print("Invalid input. Please enter the hour and am/pm as specified.")
else:
current_hour, am_pm = input_parts
if not hours_ahead.isdigit():
print("Invalid input. Please enter a valid number of hours.")
else:
hours_ahead = int(hours_ahead)
total_hours = (current_hour + hours_ahead) % 12 or 12
new_am_pm = "pm" if total_hours >= 12 else "am"
OUTPUT-