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
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...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
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...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such as a cube, sphere, cylinder and cone. In this file there should be four methods defined. Write a method named cubeVolFirstName, which accepts the side of a cube in inches as an argument into the function. The method should calculate the volume of a cube in cubic inches and return the volume. The formula for calculating the volume of a cube is given below....
Assignment 2 Programming Language: Python 2 Preface: Create a new python file with a name of...
Assignment 2 Programming Language: Python 2 Preface: Create a new python file with a name of your choosing (ex: “assignment2.py”). At the top of the file paste the following: import numpy as np import random class Base_Obstacle_Map(object):     def __init__(self, length, width):         self.map = np.zeros((length, width), dtype=np.uint8)     def add_obstacle(self, x, y):         self.map[x, y] = 1     def remove_obstacle(self, x, y):         self.map[x, y] = 0     def display_map(self):         try:             import matplotlib.pyplot as plt             plt.imshow(self.map)...
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...
Your assignment is to implement a computer program in C++ to implement the below application. According...
Your assignment is to implement a computer program in C++ to implement the below application. According to Dummies.com the following algorithm determines the amount of paint you need to paint the walls of a four-sided room: 1. Add together the length of each wall. (For example, if each of the four walls are 14, 20, 14, 20 feet respectively then the total length is 14 + 20 + 14 + 20 = 68 feet) 2. Multiply the sum by the...
(For Python) Evaluating Postfix Arithmetic Expressions. In this project you are to implement a Postfix Expression...
(For Python) Evaluating Postfix Arithmetic Expressions. In this project you are to implement a Postfix Expression Evaluator as described in section 7-3b of the book. The program should ask the user for a string that contains a Postfix Expression. It should then use the string's split function to create a list with each token in the expression stored as items in the list. Now, using either the stack classes from 7.2 or using the simulated stack functionality available in a...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… The program should now determine the number of classes, and should do so by generating a unique, random number. Replace taking user input for the number of rooms with a computer generated number...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT