Programs The programs you will write for this project are copied with permission
ID: 3712533 • Letter: P
Question
Programs The programs you will write for this project are copied with permission, or adapted from, the exercises in A Beginner’s Python Tutorial. Each program should be written, tested, and debugged. The first two programs should also be fully commented, with each line documented by a descriptive comment. The remaining programs should have a single comment line at the beginning to describe the function of the program. All programs should start with a display of your name, student id#, and the program number and name. When storing the files, name them as prog1.py, prog2.py, etc. EXAMPLE OF AUTHOR/PROGRAM INFORMATION OUTPUT Program author: B. Rubble ID#: 1234567 Program 1—Math Functions PROGRAM 1—MATH FUNCTIONS Write an algorithm for a program that shows the use of all six math functions. Write, test, and debug the program using Python. SAMPLE OUTPUT (not including author/program information) ADDITION: 2+2=4 SUBTRACTION: 4-2=2 MULTIPLICATION: 4*2=8 DIVISION: 4/2=2 EXPONENT: 2**3=8 REMAINDER: 5%2=1 PROGRAM 2—USING INPUT Write an algorithm for a program that receives, as input from the user, 2 string variables and 2 integer variables; then joins together and displays the combined strings; and finally multiplies the two numbers on a new line. Write, test, and debug the program using Python. SAMPLE OUTPUT (not including author/program information) Input string 1? Billy Input String 2? Bob Input integer A? 23 Input integer B? 2 BillyBob 46 PROGRAM 3—LOOPS AND IF CONDITIONS Write a program that requests a password after the author/program information is displayed. Make the password "hello". The program should then ask the user for their name: if the name entered is the same as your name, the program should respond with "What a great name!"; if they enter "Madonna" or "Cher", the program should respond "May I have your autograph, please?". For any other input, the program should respond with "(input name), that’s a nice name". SAMPLE OUTPUT (including author/program information) Program author: Barney Rubble ID#: 1234567 Program 3—LOOPS AND IF CONDITIONS Password? unicorn Password? opus Password? hello Welcome to the second half of the program! What is your name? Barney What a great name! ALTERNATE OUTPUTS What is your name? Cher May I have your autograph, please? What is your name? Bill Bill, that’s a nice name. PROGRAM 4—FUNCTIONS Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangle, and a circle (3.14 * radius**2). This program should include a menu interface that has ‘exit the program’ as one of its choices. SAMPLE PROGRAM EXECUTION Area.py #This program calculates the perimeter and area of a rectangle print "Calculate information about a rectangle" length = input("Length:") width = input("Width:") print "Area",length*width print "Perimeter",2*length+2*width SAMPLE OUTPUT (not including author/program information) CALCULATIONS MENU 1) AREA (SQUARE) 2) AREA (RECTANGLE) 3) AREA (CIRCLE) 4) PERIMETER (SQUARE) 5) PERIMETER (RECTANGLE) 6) PERIMETER (CIRCLE) 7) EXIT INPUT MENU CHOICE (1,2,3,4,5,6 OR 7)? 2 YOU HAVE CHOSEN AREA (RECTANGLE) INPUT WIDTH? 8 INPUT LENGTH? 4 AREA IS 32 INPUT MENU CHOICE? Packaging and Submitting When submitting your project, use WinZip or a compatible program to compress all the required files into a single archive. There should be four files in total: prog1.py, prog2.py, prog3.py, and prog4.py.
Explanation / Answer
NOTE: I have completed the program for your assignment. Please check and let me know if you have any questions. I will acknowledge back with a response within 24 hours. Thanks for your patience.
Prog.1 Code:
Unix Terminal> cat prog1.py
#!/usr/local/bin/python3
def main():
# taking 2 numbers as input from the user
print('Enter two numbers as input?')
first_num = input('Enter first number: ')
second_num = input('Enter second number: ')
first_num = int(first_num)
second_num = int(second_num)
# performing the six mathematical functions on the two numbers read
add = first_num + second_num
sub = first_num - second_num
mult = first_num * second_num
div = first_num / second_num
exp = first_num ** second_num
rem = first_num % second_num
# displaying resulting of all 6 operations
first_num = str(first_num)
second_num = str(second_num)
print('ADDITION: ' + first_num + '+' + second_num + '=' + str(add))
print('SUBTRACTION: ' + first_num + '-' + second_num + '=' + str(sub))
print('MULTIPLICATION: ' + first_num + '*' + second_num + '=' + str(mult))
print('DIVISION: ' + first_num + '/' + second_num + '=' + str(div))
print('EXPONENTIATION: ' + first_num + '**' + second_num + '=' + str(exp))
print('REMAINDER: ' + first_num + '%' + second_num + '=' + str(rem))
if __name__=='__main__':
main()
Unix Terminal>
Code linK: pasted.co/c2813369
Code output snapshot:
Unix Terminal> python3 prog1.py
Enter two numbers as input?
Enter first number: 10
Enter second number: 20
ADDITION: 10+20=30
SUBTRACTION: 10-20=-10
MULTIPLICATION: 10*20=200
DIVISION: 10/20=0.5
EXPONENTIATION: 10**20=100000000000000000000
REMAINDER: 10%20=10
Unix Terminal>
Prog.2 Code:
Unix Terminal> cat prog2.py
#!/usr/local/bin/python3
def main():
print('Input string 1? ')
str1 = input()
print('Input string 2? ')
str2 = input()
print('Input integer A? ')
int1 = int(input())
print('Input integer B? ')
int2 = int(input())
int_mul = int1 * int2
print(str1 + str2 + ' ' + str(int_mul))
if __name__=='__main__':
main()
Unix Terminal>
code link: pasted.co/d74f7873
Code output snapshot:
Unix Terminal> python3 prog2.py
Input string 1?
Billy
Input string 2?
Bob
Input integer A?
23
Input integer B?
2
BillyBob 46
Unix Terminal> python3 prog2.py
Input string 1?
hello
Input string 2?
world
Input integer A?
10
Input integer B?
90
helloworld 900
Unix Terminal>
Prog.3 Code:
Unix Terminal> cat prog3.py
#!/usr/local/bin/python3
def main():
# running loop infinitely
while True:
# displaying author infomraiton
print('Program author: Barney Rubble ID#: 1234567')
# taking password
print('Password? unicorn Password? opus Password? ')
passwd = input()
# if password matches with hello
if passwd == 'hello':
# taking user name
print('Welcome to the second half of the program!')
print('What is your name? ')
name = input()
# based on user name responding with different messages
if name == 'Barney':
print('What a great name!')
elif name == 'Madonna' or name == 'Cher':
print('May I have your autograph, please?')
else:
print(name + ', that’s a nice name.')
# if password does not match
else:
print('Please enter the correct password')
if __name__=='__main__':
main()
Unix Terminal>
Code link: pasted.co/b8764a73
Code output snapshot:
Unix Terminal> python3 prog3.py
Program author: Barney Rubble ID#: 1234567
Password? unicorn Password? opus Password?
hello
Welcome to the second half of the program!
What is your name?
Barney
What a great name!
Program author: Barney Rubble ID#: 1234567
Password? unicorn Password? opus Password?
Cher
Please enter the correct password
Program author: Barney Rubble ID#: 1234567
Password? unicorn Password? opus Password?
hello
Welcome to the second half of the program!
What is your name?
Cher
May I have your autograph, please?
Program author: Barney Rubble ID#: 1234567
Password? unicorn Password? opus Password?
hello
Welcome to the second half of the program!
What is your name?
Carmen
Carmen, that’s a nice name.
Program author: Barney Rubble ID#: 1234567
Password? unicorn Password? opus Password?
Prog.4 Code:
Unix Terminal> cat prog4.py
#!/usr/local/bin/python3
def main():
while True:
print('CALCULATIONS MENU')
print('1) AREA(SQUARE)')
print('2) AREA(RECTANGLE)')
print('3) AREA(CIRCLE)')
print('4) PERIMETER(SQUARE)')
print('5) PERIMETER(RECTANGLE)')
print('6) PERIMETER(CIRCLE)')
print('7) EXIT INPUT MENU CHOICE (1,2,3,4,5,6 OR 7)? ')
choice = int(input())
if choice == 1:
area_of_square()
elif choice == 2:
area_of_rect()
elif choice == 3:
area_of_circle()
elif choice == 4:
per_of_square()
elif choice == 5:
per_of_rect()
elif choice == 6:
per_of_circle()
elif choice == 7:
break
def area_of_square():
print('YOU HAVE CHOSEN AREA (SQUARE)')
print('INPUT SIDE? ')
side = int(input())
area = side ** 2
print('AREA is', str(area))
def area_of_rect():
print('YOU HAVE CHOSEN AREA (RECTANGLE)')
print('INPUT LENGTH? ')
length = int(input())
print('INPUT WIDTH? ')
width = int(input())
area = length * width
print('AREA is', str(area))
def area_of_circle():
print('YOU HAVE CHOSEN AREA (CIRCLE)')
print('INPUT RADIUS? ')
radius = int(input())
area = 3.14 * (radius ** 2)
print('AREA is', str(area))
def per_of_square():
print('YOU HAVE CHOSEN PERIMETER (SQUARE)')
print('INPUT SIDE? ')
side = int(input())
perimeter = 4 * side
print('PERIMETER is', str(perimeter))
def per_of_rect():
print('YOU HAVE CHOSEN PERIMETER (RECTANGLE)')
print('INPUT LENGTH? ')
length = int(input())
print('INPUT WIDTH? ')
width = int(input())
perimeter = 2 * ( length + width)
print('PERIMETER is', str(perimeter))
def per_of_circle():
print('YOU HAVE CHOSEN PERIMETER (CIRCLE)')
print('INPUT RADIUS? ')
radius = int(input())
circumference = 2 * 3.14 * radius
print('PERIMETER is', str(circumference))
if __name__=='__main__':
main()
Unix Terminal>
Code link: pasted.co/a33bf09a
Code output snapshot:
Unix Terminal> python3 prog4.py
CALCULATIONS MENU
1) AREA(SQUARE)
2) AREA(RECTANGLE)
3) AREA(CIRCLE)
4) PERIMETER(SQUARE)
5) PERIMETER(RECTANGLE)
6) PERIMETER(CIRCLE)
7) EXIT INPUT MENU CHOICE (1,2,3,4,5,6 OR 7)?
2
YOU HAVE CHOSEN AREA (RECTANGLE)
INPUT LENGTH?
8
INPUT WIDTH?
4
AREA is 32
CALCULATIONS MENU
1) AREA(SQUARE)
2) AREA(RECTANGLE)
3) AREA(CIRCLE)
4) PERIMETER(SQUARE)
5) PERIMETER(RECTANGLE)
6) PERIMETER(CIRCLE)
7) EXIT INPUT MENU CHOICE (1,2,3,4,5,6 OR 7)?
1
YOU HAVE CHOSEN AREA (SQUARE)
INPUT SIDE?
8
AREA is 64
CALCULATIONS MENU
1) AREA(SQUARE)
2) AREA(RECTANGLE)
3) AREA(CIRCLE)
4) PERIMETER(SQUARE)
5) PERIMETER(RECTANGLE)
6) PERIMETER(CIRCLE)
7) EXIT INPUT MENU CHOICE (1,2,3,4,5,6 OR 7)?
6
YOU HAVE CHOSEN PERIMETER (CIRCLE)
INPUT RADIUS?
2
PERIMETER is 12.56
CALCULATIONS MENU
1) AREA(SQUARE)
2) AREA(RECTANGLE)
3) AREA(CIRCLE)
4) PERIMETER(SQUARE)
5) PERIMETER(RECTANGLE)
6) PERIMETER(CIRCLE)
7) EXIT INPUT MENU CHOICE (1,2,3,4,5,6 OR 7)?
7
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.