1. Write a Python program to convert temperatures to and from celsius, fahrenheit.
[ Formula : c/5 = f-32/9 [ where the c = temperature in Celsius and f = temperature in Fahrenheit]
Expected Output :
60°C is 140 in Fahrenheit
45°F is 7 in Celsius Write a Python program to convert temperatures to and from Celsius,
Fahrenheit.
Python: Centigrade and Fahrenheit Temperatures:
The centigrade scale, which is also called the Celsius scale, was developed by Swedish
astronomer Andres Celsius. In the centigrade scale, water freezes at 0 degrees and boils at 100
degrees. The centigrade to Fahrenheit conversion formula is:
Fahrenheit and centigrade are two temperature scales in use today. The Fahrenheit scale was
developed by the German physicist Daniel Gabriel Fahrenheit . In the Fahrenheit scale, water
freezes at 32 degrees and boils at 212 degrees.
C = (5/9) * (F - 32)
Where F is the Fahrenheit temperature. You can also use this Web page to convert Fahrenheit
temperatures to centigrade. Just enter a Fahrenheit temperature in the text box below, then click
on the Convert button.
temp = input("Input the temperature you like to convert? (e.g., 45F, 102C etc.) : ")
degree = int(temp[:-1])
i_convention = temp[-1]
if i_convention.upper() == "C":
result = int(round((9 * degree) / 5 + 32))
o_convention = "Fahrenheit"
elif i_convention.upper() == "F":
result = int(round((degree - 32) * 5 / 9))
o_convention = "Celsius"
else:
print("Input proper convention.")
quit()
print("The temperature in", o_convention, "is", result, "degrees.")
Output:
2. Write a Python program to guess a number between 1 to 9.
Note : User is prompted to enter a guess. If the user guesses wrong then the prompt appears again until
the guess is correct, on successful guess, user will get a "Well guessed!" message, and the program will
exit
import random
target_num, guess_num = random.randint(1, 10), 0
while target_num != guess_num:
guess_num = int(input('Guess a number between 1 and 10 until you get it right : '))
print('Well guessed!')
Output:
3. Write a Python program to construct the following pattern, using a nested for loop.
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
n=5;
for i in range(n):
for j in range(i):
print ('* ', end="")
print('')
for i in range(n,0,-1):
for j in range(i):
print('* ', end="")
print('')
4. Write a Python program to calculate a dog's age in dog's years.
Note: For the first two years, a dog year is equal to 10.5 human years. After that, each dog year equals 4
human years.
Expected Output:
Input a dog's age in human years: 15
The dog's age in dog's years is 73
h_age = int(input("Input a dog's age in human years: "))
if h_age < 0:
print("Age must be positive number.")
exit()
elif h_age <= 2:
d_age = h_age * 10.5
else:
d_age = 21 + (h_age - 2)*4
print("The dog's age in dog's years is", d_age)
Output:
Output: