Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 5 Instructions For Lab 5, you will be writing a more complex modular program

ID: 3777104 • Letter: L

Question

Lab 5 Instructions

For Lab 5, you will be writing a more complex modular program that uses at least two arrays, and has at least one loop that accesses the data in the arrays.

As long as your program satisfies the requirements listed below, you are free to design and write any type of program that you care to. You are encouraged to be creative, and pick something that has meaning for you, because you'll have more fun. Feel free to create a more complex version of the program you did in an earlier lab, as long as it meets all of the additional requirements below.

Requirements

Your lab submission should consist of a single Python file, Lab5.py, uploaded to the Lab 5 dropbox. The Lab5.py file should meet all of the following requirements:

Your name given as the author.

Comments including a brief description of the program, Input List and Output List, and full pseudocode. Place the pseudocode for each module above the module's Python code.

The program must have at least one input and at least one output.

All user input must be validated. This means the user is not allowed to just enter any value. You must check the value, and ask the user to enter it again, and repeat this loop until the user enters a valid value.

Your program must use at least two arrays in meaningful ways. These two arrays can contain any type of values, as long as they are both used meaningfully in your program.

Your program must have at least one loop that accesses the data in the arrays. For example, you might have an input loop that asks the user to enter the data one element at a time (be sure to validate the data). Or, you might have an output loop that writes the data to the console. Or, you might have a calculation loop that calculates one or more values, such as minimum value, maximum value, averages, and so on. You can have all three of those types of loops, if you want (the Lab 5 walkthrough video shows an example of each).

Your program should be organized into separate modules. Each module should be "cohesive" and should only do one thing.

Use parameters and arguments to pass values into your modules (don't use global variables).

The Python code should run correctly, and the logic should match your pseudocode.

An example program

Here is an optional walkthrough video showing this complete process, courtesy of Marc Goodman. If you already understand the pseudocode and Python, and already know what kind of program you want to write, feel free to skip it. If you're unsure of the exact Python or pseudocode syntax you'll need, or if you're looking for some ideas on what to write, you might find the video helpful. Here is a link to the complete Python program (including pseudocode, input list, and output list in comments) from the video.

OK Safari ...oo 1:44 PM Sheet 1 Table 1 author z Marc put List: names, volumes, proofs Output List: names, volumes, proofs, total volume, total volume alcohol, percent ingredient, percent. alcohol, counter Constant Integer MAX INGREDIENTS 256 MAX INGREDIENTS 256 Function String get st String prompt) Declare String value Display prompt Input value return value End Function def get string (prompt): value value input(prompt) return value f Function Real get real String promp Declare String value Display prompt Input value While not value is a real number Display value, "is not a number. Please try again." Display prompt input value End While Return value End Function def valid real(value): float(value) return True except return False def get real(prompt) value

Explanation / Answer

MAX_INGREDIENTS = 256

def get_string(prompt):
   value=""
   value = input(prompt)
   return value
  
def valid_real(value):
       try:
           float(value)
           return True
       except:
           return False

def get_real(prompt):
   value=""
   value = input(prompt)
   while not valid_real(value):
       print(value,"is not a number. Please try again.")
       value = input(prompt)
   return float(value)

def y_or_n(prompt)
   value=""
   value=input(prompt)
   while True:
       if value =="Y" or value == "y":
           return True
       elif value == "N" or value == "n":
           return False
       else:
           print("PLese enter Y or N!")
           value = input(prompt)

def get_ingredients(names, volumes,proofs):
   done = false
   counter = 0
   while not done:
       names(counter) = get_string("What is name of the ingredient?")
       volumes(counter) = get_real("How much"+names(counter)+" do you want?")
       proofs(counter) = get_real("What proof is "+names(counter)+ "?")
       counter = counter+1
       done = y_or_n("Are you finished enter ingredient(Y/N)?")
   return counter

def calculate_totals(number_of_ingredients,volumes,proofs):
   counter = 0
   total_volume = 0.0
   total_volume_alcohol = 0.0
  
   while counter<number_of_ingredients:
       total_volume = total_volume + volumes[counter]
       total_volume_alcohol = total_volume_alcohol+volumes[counter]*proofs[counter]/200.0
       counter = counter+1
   return total_volume,total_volume_alcohol
  
def display_recipe(number_of_ingredients,names,volumes,proofs,total_volume,total_volume_alcohol):
   counter = 0
   percent_alcohol = 0.0
   percent_ingredient = 0.0
  
   while counter < number_of_ingredients:
       percent_ingredient = 100 * volumes[counter]/total_volume
       print("Ingredient",(counter+1),"-","{:.2f}".format(percent_ingredient),"of total;",volumes[counter],"ml of",names[counter],"at",proofs[counter],"proof)
       counter = counter+1
       percent_alcohol = 100*total_volume_alcohol/total_volume
       print("Total volume",total_volume)
       print("Totalalcohol,",total_volume_alcohol)
       print("Total alcohol percent","{:.2f}".format(percent_alcohol))
      
def drink_recipe():
   names = ["for x in range(MAX_INGREDIENTS")]
   volumes = [0.0 for x in range(MAX_INGREDIENTS)]
   proofs = [0.0 for x in range(MAX_INGREDIENTS)]
   number_of_ingredients = 0
   total_volume = 0.0
   total_volume_alcohol = 0.0