40.0k views
4 votes
If we were ever to use this pizza ordering program again, we would want to use mainline logic to control its execution. The way it is written, it will automatically execute as soon as it is imported.

Re-write this program, enclosing the top level code into the main function. Then execute the main function. Enter appropriate input so the output matches that under Desired Output.
# Define Function
def pizza(meat="cheese", veggies="onions"):
print("You would like a", meat, "pizza with", veggies)
# Get the User Input
my_meat = input("What meat would you like? ")
my_veggies = input("What veggie would you like? ")
# Call the Function
pizza(meat=my_meat, veggies=my_veggies)
desired output:
What meat would you like? cheese
What veggie would you like? onions
You would like a cheese pizza with onions
2) We want to generate 5 random numbers, but we need the random module to do so. Import the random module, so the code executes.
# Define main function
def main():
print("Your random numbers are:")
for i in range(5):
print(random.randint(1, 10))
# Call main function
main()
desired output:
Your random numbers are:
[x]
[x]
[x]
[x]
[x]

User Luke Kim
by
3.1k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

The Python program has been adjusted. Now the pizza ordering function is called from the main() method and not as soon as the file is imported. Also, the random package has been imported so that the random.randint(1,10) can correctly create the 5 random numbers needed. The code has been tested and the output can be seen in the attached image below, correctly matching the desired output.

import random

def pizza(meat="cheese", veggies="onions"):

print("You would like a", meat, "pizza with", veggies)

def main():

# Get the User Input

my_meat = input("What meat would you like? ")

my_veggies = input("What veggie would you like? ")

# Call the Function

pizza(meat=my_meat, veggies=my_veggies)

print("Your random numbers are:")

for i in range(5):

print(random.randint(1, 10))

# Call main function

main()

If we were ever to use this pizza ordering program again, we would want to use mainline-example-1
User Vanza
by
3.4k points