In: Computer Science
4. On a roulette wheel, the pockets are numbered from 0 to 36. The colours of the pockets are as follows:
• Pocket 0 is green.
• For pockets 1 through 10, the odd-numbered pockets are red and the even-numbered pockets are black.
• For pockets 11 through 18, the odd-numbered pockets are black and the even-numbered pockets are red.
• For pockets 19 through 28, the odd-numbered pockets are red and the even-numbered pockets are black.
• For pockets 29 through 36, the odd-numbered pockets are black and the even-numbered pockets are red.
Write a function, pocket_colour that takes an integer as parameter and returns the colour corresponds to the pocket number.
"The function should return the string "Error" if the user enters a number that is outside the range of 0 through 36.
Write a testing program named t04.py that tests your function by asking the user to enter a pocket number and displays whether the pocket is green, red, or black.
A sample run:
Enter a pocket number: 0
The selected pocket is green.
t04.py
'''Function pocket_colour that takes an integer as parameter'''
def pocket_colour(num):
#Condition where Pocket 0 is green
if num == 0:
return "Green"
#Condition for pockets 1 through 10 (odd-numbered pockets are red and even-numbered pockets are black)
elif num >= 1 and num <= 10:
if(num % 2 == 0): #Checking whether number is even or not.
return "Black"
return "Red"
#Condition for pockets 11 through 18 (odd-numbered pockets are black and even-numbered pockets are red)
elif num >= 11 and num <= 18:
if(num % 2 == 0): # Checking whether number is even or not.
return "Red"
return "Black"
#Condition for pockets 19 through 28 (odd-numbered pockets are red and even-numbered pockets are black)
elif num >= 19 and num <= 28:
if(num % 2 == 0): # Checking whether number is even or not.
return "Black"
return "Red"
#Condition for pockets 11 through 18 (odd-numbered pockets are black and even-numbered pockets are red)
elif num >= 29 and num <= 36:
if(num % 2 == 0): # Checking whether number is even or not.
return "Red"
return "Black"
#Condition to display error if user enter integer outsides the range of 0 through 36.
else:
return "Error"
'''Testing Program that tests function pocket_colour and displays output accordingly.'''
def main():
num = int(input("Enter pocket number: ")) # Converting input string to integer.
print(pocket_colour(num))
#Invoking main function
if __name__ == "__main__":
main()
Refer to the code snippet above for a detailed explanation of the solution. The testing program is fairly simple as the function does most of the work. Hence function and testing program can be kept in the same file. Revert back in the comments if you need further clarification. You can copy the code and test directly in an online ide. All the best!