![]()
Chapter 10: The solution!
I hope you really tried to create the programs by yourselves because nothing beats practice! 🥵
If that's the case, there may be several ways to program the same thing. The correction I'm suggesting is just one possibility among others. If your program is different but works, that doesn't mean you're wrong. What's interesting is to compare the different methods. To illustrate this, I'll give you two different examples for the casino program. 😉
Program 1: Baggage labeling |
|
You will program automatic baggage tagging software for the new Meruvia Airport. The program will ask the user to enter the number of bags to tag as an integer (int). Example result for 4 bags: MERUVIA – Checked Baggage No. 00 |
Here is a solution:
#Automatic Baggage Tagging Program for
#Meruvia International Airport
print ("Hello, Meruvia International Airport.")
#We ask the user for information: number of bags + label
nb = int (input("Please enter the number of bags to label:"))
label = str(input("Please enter the content of the label:"))
#Optional:
print("Starting labeling...")
#"For" loop that prints all labels
for i in range (nb):
print(label + " # " + str(i))
#Goodbye phrase
print("Labeling complete! Have a nice day.")
There you go! The program wasn't very complicated but had the merit of allowing you to reuse everything we've already seen. 😉
I added sentences to greet the user or say goodbye, because it's still more professional, but it was optional. 😋
Now let's move on to the casino program!
Program 2: AI Dealer |
|
Meruvia's new Casino Royale asks you to design a program that rolls dice randomly, without cheating of course! 🙃 You will use the following code to generate a random number:
You will ask the user to enter an integer (int) which will be the maximum value of your dice roll (you will therefore have to change this 99 with a variable...). You will roll the dice with the above function and display the value. Finally, you will put this program in a while loop asking on each turn if the user wants to continue rolling the dice. Otherwise you will exit the program. Good luck! 😇 |
This program was a bit more complex because it used the random library which allows you to generate random numbers. But since I gave you the code, normally, it should have worked! 😅
Here is a first version, very academic:
# Meruvia Royal Casino: dice throwing program
#We import the random library which allows us to generate random numbers
import random
#We create a variable to exit our infinite loop
continuing = True
#Infinite loop
while continuing:
# The user is asked if he wants to generate a new die roll
answer = input("Hello, do you want to generate a new die roll? Y / N ")
if answer == "Y":
#The user is asked to enter the maximum value of the dice
maxDice = int(input("Please enter the maximum value of a die roll: "))
# Returns a random number between 1 and the entered value (inclusive)
nb = random.randint(1, maxDice)
# Display the result of the die roll
print("Result of the die roll: " + str(nb))
# If the player no longer wants to play, we quit
else:
print("The Royal Casino of Meruvia wishes you an excellent day!" )
continuing = False
And here is a second version, simpler / shorter but which does the same thing:
# Meruvia Royal Casino: dice throwing program
import random
print("The Royal Casino of Meruvia welcomes you!")
continuing = True
#Infinite loop
while continuing:
#The user is asked to enter the max value of the dice
maxDice = int(input("Please enter the maximum value of a die roll: "))
# Display the result of the die roll
print("Result of the die roll: " + str(random.randint(1, maxDice)))
# The user is asked if he wants to generate a new die roll
if str(input("Do you want to generate a new die roll? Y / N ")) == "N":
# If the player doesn't want to play anymore, we quit
print("The Royal Casino of Meruvia wishes you a great day!")
continuing = False
This second one is shorter because it concatenates several lines into one and optimizes the code by only handling the "No" answer (because the program continues otherwise, so there is no point in handling the "Yes" answer). It's smarter! 😜
Here we go, the next chapter will be the final test for this initiation! 😉
Revise carefully! ☺️

English
Français