Describe a situation in which you would want to use integer division in a program. Then, write the line of code that would use integer division.

Describe a situation in which you would want to use modulus division in a program. Then, write the line of code that would use modulus division.
A customer has hired you to code a cooking mystery game. Here is part of the narrative: “When the player enters the kitchen, they can choose to preheat the oven or chop vegetables. If they choose to preheat the oven, they will get a message that the oven is broken. If they choose to chop the vegetables, they will get a message that the knife is missing.” Write the code for this situation.
Write three lines of code using a sequence (no iteration or selection) that would be appropriate for a story or game.
Will the output for each of these print statements be the same? Explain your answer.

1 answer

Integer Division

Situation: Suppose you're developing a program that distributes a certain number of cookies among a group of children, and you want to find out how many cookies each child will get without keeping any fractions. For example, if you have 25 cookies and want to share them among 4 children, you would use integer division.

Line of Code:

cookies_per_child = 25 // 4  # This results in 6 cookies per child

Modulus Division

Situation: Let's say you want to determine whether a number is even or odd. You would use modulus division to find the remainder when dividing by 2. If the number has a remainder of 0, it is even; otherwise, it is odd.

Line of Code:

is_even = (number % 2 == 0)  # This will be True if the number is even

Cooking Mystery Game Code

Here's how you might implement the simple narrative described:

user_choice = input("Choose an action: 'preheat the oven' or 'chop vegetables': ")

if user_choice == "preheat the oven":
    print("The oven is broken.")
elif user_choice == "chop vegetables":
    print("The knife is missing.")
else:
    print("Invalid choice.")

Three Lines of Code using a Sequence

print("You enter the kitchen and are greeted by the smell of fresh ingredients.")
print("You hear a strange noise coming from the pantry.")
print("The kitchen is filled with colorful vegetables and fragrant herbs.")

Will the Output for Each of These Print Statements Be the Same?

Answer: No, the output for each of these print statements will not be the same. Each print statement contains a unique string message that describes different aspects of the scenario. When executed sequentially, each call to print will result in a different line being printed to the console, conveying varied information to the player about the game environment.