Question

Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features...

Module 4 Assignment 1: Pseudocode & Python with Variable Scope

Overview
Module 4 Assignment 1 features the design of a calculator program using pseudocode and a Python program that uses a list and functions with variable scope to add, subtract, multiply, and divide. Open the M4Lab1ii.py program in IDLE and save it as M4Lab1ii.py with your initials instead of ii.
Each lab asks you to write pseudocode that plans the program’s logic before you write the program in Python and to turn in three things: 1) the pseudocode, 2) a screenshot of the output, and 3) the Python program.

Instructions
Write pseudocode for a Python program that 1) defines a list to store the choice of +, -, *, and /, 2) defines functions to support the calculations, and 3) calls them in response to user inputs. Test each of the four calculations, and turn in the pseudocode, the Python program, and a screenshot of your output displaying the four tests in the Python shell.

Requirements for Your M4 Assignment 1 Pseudocode & Python with Variable Scope

Complete a program in pseudocode and Python that performs the following tasks.

Open the file called M4Lab1ii.py linked below these instructions in your M4 Content module in IDLE.
Save as M4Lab1ii.py. Replace ii with your initials. [example for someone with the initials cc: M4Lab1cc.py]
Complete Steps 1-7 as requested within the code’s comments.
Run your program and test all four calculations, then exit the program.
Save your program as M4Lab1ii.py using your initials.
Take a screenshot of your program’s output from the Python shell.
Save your pseudocode as M4Lab1ii.docx. Replace ii with your initials.
Insert a screenshot of your program output in the pseudocode’s Word file.

How to Complete Your M4 Assignment 1 Pseudocode & Python with Variable Scope

Write your pseudocode and save it as M4Lab1ii.docx. Replace ii with your initials.
Write your program and save it as M4Lab1ii.py. Replace ii with your initials.
Take a screenshot of your program’s output and Insert it in M4Lab1ii.docx
Upload both M4 Lab documents (Word and Python) to M4 Assignment 1 Pseudocode & Python with Variable Scope Assignment Submission Folder. .

# Calculator Program
# M4Lab1ii.py for M4 Assignment 1
# C. Calongne, 01/19/19

# Seven steps to complete in this lab:
# Define a list to store the choices for +, -, *, /
# Define 3 functions with local variables
# Define three elif statements that call the functions for -, *, and /

# Local variables are visible in the functions
# Pass the input values into them.
# Continue calculating until the user presses "n"

print("Welcome to your Calculator program")

# The add function adds two numbers
def add(x, y):
return x + y
# local scope; only the add() function sees x and y

#write the other three functions for subtract, multiply and divide

# Step 1: Write a subtract function that subtracts two numbers


# Step 2: Write a multiply function that multiplies two numbers


# Step 3: Write a divide function that divides two numbers


# Start calculating
# Step 4: define a list called choice with + - * / in it


keep_going = ["y", "n"] # only tests for "n"

while keep_going != "n":

num1 = float(input("Enter first number: "))
choice = input("What kind of calculation? Choose one of: +, -, *, / ")
num2 = float(input("Enter second number: "))

if choice == "+":
# choice is a list with the string value for "+"
# The test for choice returns the value True or False. If true:

print(num1, choice, num2,"=", add(num1,num2))
# calls the add function and passing it two parameters

# finish the rest of the if statement for the other 3 calculations
# Step 5: write an elif for subtract


# Step 6: write an elif for multiply


# Step 7: write an elif for divide

  
else:
print("Invalid input")

keep_going = input("Calculate? y or n? ")
print("Closing the calculator.")

Homework Answers

Answer #1

Pseudo code

Start
   Declare list with values of operations performing "+,-,* or /"
   Declare list with values of loop repetition "y,n"
   While:
      input first number
      input choice of operation
      input second number
      IF choice==operation list first value
          display firstnumber, choice, secondnumber,"=", add(num1,num2)
      ELSE IF choice==operation list second value
          display firstnumber, choice, secondnumber,"=", sub(num1,num2)
      ELSE IF choice==operation list third value
          display firstnumber, choice, secondnumber,"=", mul(num1,num2)
      ELSE IF choice==operation list fourth value
          display firstnumber, choice, secondnumber,"=", div(num1,num2)
      ElSE
          display "Invalid input"
      input loop repetition ""Calculate? y or n? "
   display ""Closing the calculator."
End

Function add(firstnum,secondnum)
   return firstnum+secondnum
End Function

Function sub(firstnum,secondnum)
   return firstnum-secondnum
End Function

Function mul(firstnum,secondnum)
   return firstnum*secondnum
End Function

Function add(firstnum,secondnum)
   return firstnum/secondnum
End Function


Program Screenshot

Program

# Calculator Program
# M4Lab1ii.py for M4 Assignment 1
# C. Calongne, 01/19/19

# Seven steps to complete in this lab:
# Define a list to store the choices for +, -, *, /
# Define 3 functions with local variables
# Define three elif statements that call the functions for -, *, and /

# Local variables are visible in the functions
# Pass the input values into them.
# Continue calculating until the user presses "n"

print("Welcome to your Calculator program")

# The add function adds two numbers
def add(x, y):
    return x + y
# local scope; only the add() function sees x and y

#write the other three functions for subtract, multiply and divide

# Step 1: Write a subtract function that subtracts two numbers
def sub(x, y):
    return x - y
# Step 2: Write a multiply function that multiplies two numbers
def mul(x, y):
    return x * y

# Step 3: Write a divide function that divides two numbers
def div(x, y):
    return x / y

# Start calculating
# Step 4: define a list called choice with + - * / in it
operations=['+','-','*','/']

keep_going = ["y", "n"] # only tests for "n"

while keep_going != "n":
    num1 = float(input("Enter first number: "))
    choice = input("What kind of calculation? Choose one of: +, -, *, / ")
    num2 = float(input("Enter second number: "))
    if choice == operations[0]:
        # choice is a list with the string value for "+"
        # The test for choice returns the value True or False. If true:
        print(num1, choice, num2,"=", add(num1,num2))
        # calls the add function and passing it two parameters
    # finish the rest of the if statement for the other 3 calculations
    # Step 5: write an elif for subtract
    elif choice == operations[1]:
        print(num1, choice, num2,"=", sub(num1,num2))
    # Step 6: write an elif for multiply
    elif choice == operations[2]:
        print(num1, choice, num2,"=", mul(num1,num2))
    # Step 7: write an elif for divide
    elif choice == operations[3]:
        print(num1, choice, num2,"=", div(num1,num2))
    else:
        print("Invalid input")
    keep_going = input("Calculate? y or n? ")
print("Closing the calculator.")

Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Draw a flowchart based on the Python code below: ch = -1 #Variable declared for taking...
Draw a flowchart based on the Python code below: ch = -1 #Variable declared for taking option entered by the user print("Enter 0 to 5 for following options: ") print("0-> Issue new ticket number.") print("1-> Assign first ticket in queue to counter 1.") print("2-> Assign first ticket in queue to counter 2.") print("3-> Assign first ticket in queue to counter 3.") print("4-> Assign first ticket in queue to counter 4.") print("5-> Quit Program.") tickets = [] #Declaring list to store...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
IN PYTHON Menu: 1. Hotdog. ($2) 2. Salad ($3)  3. Pizza ($2) 4.Burger ($4) 5.Pasta ($7) Write...
IN PYTHON Menu: 1. Hotdog. ($2) 2. Salad ($3)  3. Pizza ($2) 4.Burger ($4) 5.Pasta ($7) Write a menu-driven program for  Food Court. (You need to use functions!) Display the food menu to a user . 5 options Use numbers for the options and for example "6" to exit. Ask the user what he/she wants and how many of it. (Check the user inputs) AND use strip() function to strip your inputs(if needed) Keep asking the user until he/she chooses the exit...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...