Assignment 2 – Python
Q 1) what will the flowing program print for inputs A to D (2 Marks for each correct answer)
RED = “red”
BLUE = “blue”
YELLOW = “yellow”
color1 = input(‘Enter the first primary color in lower case letters: ‘)
color2 = input(‘Enter the second primary color in lower case letters: ‘)
if color1 != RED and color1 != BLUE and color1 != YELLOW:
print(‘Error: The first color you entered is invalid.’)
elif color2 != RED and color2 != BLUE and color2 != YELLOW:
print(‘Error: The second color you entered is invalid.’)
elif color1 == color2:
print(‘Error: The two colors you entered are the same.’)
else:
if color1 == RED:
if color2 == BLUE:
print(‘purple’)
else:
print(‘orange’)
elif color1 == BLUE:
if color2 == RED:
print(‘purple’)
else:
print(‘green’)
else:
if color2 == RED:
print(‘orange’)
else:
print(‘green’)
What will the program print for the following inputs A to D
Input A
Enter the first primary color in lower case letters: red
Enter the second primary color in lower case letters: blue
Input B
Enter the first primary color in lower case letters: yellow
Enter the second primary color in lower case letters: yellow
Input C
Enter the first primary color in lower case letters: yellow
Enter the second primary color in lower case letters: red
Input D
Enter the first primary color in lower case letters: yellow
Enter the second primary color in lower case letters: purple
Q2) Write a program that will accept input number as seconds, Converts this number in to full minutes and seconds, and print the answer as shown below. The program also converts the seconds to full hours and seconds if the number of seconds entered is greater that one hour. (12 marks)
The example output is as shown below.
Enter the number of seconds: 3600
3600 seconds are equal to:
60 full minute(s) and 0 seconds.
1 full hour(s) and 0 seconds.
Enter the number of seconds: 6000
6000 seconds are equal to:
100 full minute(s) and 0 seconds.
1 full hour(s) and 2400 seconds.
Enter the number of seconds: 60
60 seconds are equal to:
1 full minute(s) and 0 seconds.
(* only prints the minutes as 60 seconds cannot be convert to full hour)
Enter the number of seconds: 65
65 seconds are equal to:
1 full minute(s) and 5 seconds.
Enter the number of seconds: 365
365 seconds are equal to:
6 full minute(s) and 5 seconds.
Enter the number of seconds: 36005
36005 seconds are equal to:
600 full minute(s) and 5 seconds.
10 full hour(s) and 5 seconds.
Answer
Question
1
Output
A
purple
Output
B
Error: The two colors you entered are the same.
Output
C
orange
Output
D
Error: The second color you entered is invalid.
Answer Part 2
# Get a number of seconds from the user.
total_seconds = int(input('Enter a number of seconds: '))
if total_seconds < 3600:
#Get the number of remaining minutes.
minutes = (total_seconds // 60) % 60
seconds = total_seconds % 60
# Display the results.
print(total_seconds,'seconds are equal to:')
print(minutes,' full minute(s) and ',seconds,' seconds')
else:
minutes = (total_seconds // 60)
seconds = total_seconds % 60
# Display the results.
print(total_seconds,'seconds are equal to:')
print(minutes,' full minute(s) and ',seconds,' seconds')
# Get the number of hours.
hours = total_seconds // 3600
seconds = total_seconds % 3600
# Display the results.
print(hours,' full hour(s) and ',seconds,' seconds')
Leave a reply