91.9k views
3 votes
g Write a function called price_of_rocks. It has no parameters. In a while loop, get a rock type and a weight from the user. Keep a running total of the price for all requested rocks. Repeat until the user wants to quit. Quartz crystals cost $23 per pound. Garnets cost $160 per pound. Meteorite costs $15.50 per gram. Assume the user enters weights in the units as above. Return the total price of all of the material. For this discussion, you should first write pseudocode for how you would solve this problem (reference pseudocode.py in the Week 7 Coding Tutorials section for an example). Then write the code for how you would tackle this function. Please submit a Python file called rocks_discussion.py that defines the function (code) and calls it in main().

1 Answer

1 vote

Answer:

The pseudocode:

FUNCTION price_of_rocks():

SET total = 0

while True:

INPUT rock_type

INPUT weight

if rock_type is "Quartz crystals":

SET total += weight * 23

elif rock_type is "Garnets":

SET total += weight * 160

elif rock_type is "Meteorite":

SET total += weight * 15.50

INPUT choice

if choice is "n":

break

RETURN total

END FUNCTION

The code:

def price_of_rocks():

total = 0

while True:

rock_type = input("Enter rock type: ")

weight = float(input("Enter weight: "))

if rock_type == "Quartz crystals":

total += weight * 23

elif rock_type == "Garnets":

total += weight * 160

elif rock_type == "Meteorite":

total += weight * 15.50

choice = input("Continue? (y/n) ")

if choice == "n":

break

return total

print(price_of_rocks())

Step-by-step explanation:

Create a function named price_of_rocks that does not take any parameters

Initialize the total as 0

Create an indefinite while loop. Inside the loop:

Ask the user to enter the rock_type and weight

Check the rock_type. If it is "Quartz crystals", multiply weight by 23 and add it to the total (cumulative sum). If it is "Garnets", multiply weight by 160 and add it to the total (cumulative sum). If it is "Meteorite", multiply weight by 15.50 and add it to the total (cumulative sum).

Then, ask the user to continue or not. If s/he enters "n", stop the loop.

At the end of function return the total

In the main, call the price_of_rocks and print

User Erlkoenig
by
5.2k points