CS134 Unit 03 Exercise: Input, output, and variables

Table of Contents


1. Solo projects, but can collaborate

On Replit this assignment is made so that you work in your own project. Hwoever, you can still collaborate with classmates and ask questions to help figure out how to finish the program.


2. Running the examples and your program

2.1. "RUN" button - How to run the examples

replit-runbutton.png

The "RUN" button in Replit is used to run the example code. You can view the example code under the examples folder.

2.2. How to run YOUR code

The replit project starts off with three code files. You can choose which one you want to work in: To run any of them, use the following:

File Language Run command
recipe.py Python python3 recipe.py
recipe.lua Lua lua recipe.lua
recipe.cpp C++ g++ recipe.cpp && ./a.out

3. Program instructions

Choose one language you want to work with (you don't need to do all 3!!).

3.1. Bring your recipe from the previous week!

If you completed your recipe from last week, copy/paste your recipe into the code file. If you didn't finish the recipe from last week, there is a starter recipe already in the code. (You can erase it if you'd like.)

Example recipe:

print( "Butter recipe" )
print( "" )
print( "INGREDIENTS" )
print( "* 1 stick - Butter" )
print( "* 5 tbsp - Salt" )
print( "" )
print( "STEPS" )
print( "1. Put butter and salt in microwave safe bowl" )
print( "2. Microwave butter" )
print( "3. Enjoy!!" )

3.2. Add variables for each ingredient

Before your recipe is displayed, for each of the ingredients listed, create a variable and store the numeric amount for each ingredient.

amount_butter = 1
amount_salt = 5

3.3. Replace amounts in print statements

Where you're printing the amount of each ingredient, update it so that it uses the variable instead of the hard-coded number.

Before
print( "* 1 stick - Butter" )
print( "* 5 tbsp - Salt" )
After (Lua or Python)
print( "*", amount_butter, "stick - Butter" )
print( "*", amount_salt, "tbsp - Salt" )
After (C++)
cout << "* " << amount_butter << " stick - Butter" << endl;
cout << "* " << amount_salt << "tbsp - Salt" << endl;

Make sure your program still runs before continuing!

3.4. Asking the user how many batches

Also before displaying the recipe, now we will ask the user how many batches they would like to make. This can be done at the very start of the program, before you create your variables.

Python
print( "Use 0.5 for half batch, 1 for normal, 2 for double batch" )
batches = float( input( "How many batches? " ) )
Lua
print( "Use 0.5 for half batch, 1 for normal, 2 for double batch" )
print( "How many batches?" )
batches = io.read( "*n" )
C++
cout << "Use 0.5 for half batch, 1 for normal, 2 for double batch" << endl;
cout << "How many batches? ";
float batches;
cin >> batches;

3.5. Adjusting ingredient amounts

Finally, we need to adjust our ingredient variables to account for the amount of batches. While assigning ingredient amounts to the variables, add * batches to multiple the original ingredient amount by the amount of batches the user wants:

amount_butter = 1 * batches
amount_salt = 5 * batches

3.6. Example output

Butter recipe

How many batches? 1

INGREDIENTS
* 1 stick - Butter
* 5 tbsp - Salt

STEPS
1. Put butter and salt in microwave safe bowl
2. Microwave butter
3. Enjoy!!
Butter recipe

How many batches? 0.5

INGREDIENTS
* 0.5 stick - Butter
* 2.5 tbsp - Salt

STEPS
1. Put butter and salt in microwave safe bowl
2. Microwave butter
3. Enjoy!!

3.7. Complete program pseudocode

  1. Program begins
  2. Display "How many batches? "
  3. Get input from keyboard, store in variable batches
  4. Create variable for each ingredient, assign original amount * batches
  5. Display recipe, use ingredient variables instead of hard-coding ingredient amounts
  6. Program end

4. Turning in the program on Canvas

  1. Copy the URL to your Replit program
  2. Locate the exercise "🏋️ Unit 03 Exercise - Input/Output and Variables" on Canvas and select "Start assignment"
  3. Paste in the URL and click "Submit assignment"

Author: Rachel Wil Sha Singh

Created: 2023-09-13 Wed 12:45

Validate