Programming

Date: 2023-02-06

If

Problem 1

Draw the flowchart and then write the code for a program that will add 3 numbers entered by the user and then determine if the sum of the numbers is greater than 15. If it is, the program will display a relevant message.

1. Begin
2.     num1, num2, num3 are float // First establish the data types
3.     Input(num1, num2, num3) // Input the numbers
4.     If ( (num1+num2+num3) > 15) // Check if the numbers
5.         Output("The numbers summed are greater than 15!") // Output
6.     End If
7. End

Python Version

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
if (num1 + num2 + num3) > 15:
	print("The numbers summed are greater than 15!")

If Then Else

Problem 3

1. Begin
2.     age, hoursWorked, wage are float
3.     name is string
4.     Input(age, hoursWorked, name)
5.     If (age < 18) Then
6.         wage <= hoursWorked * 5.6
7.     Else
8.         wage <= hoursWorked * 7.8
9.     End If
10.    Output(age,hoursWorked, name, wage)
11. End

Python Version

name = input("What is the worker's name? ")
age = float(input("How old is the worker? "))
hoursWorked = float(input("How many hours did the worker work? "))
wage = None
if age < 18:
	wage = hoursWorked * 5.6
else:
	wage = hoursWorked * 7.8
print("{} worked {} hours. They are {} years old, and as such earn ${}".format(name, hoursWorked, age, wage))

Case

Problem 1

1. Begin
2.     copiesNeeded, totalPrice are float
3.     Input(copiesNeeded)
4.     Case copiesNeeded of
5.         < 1000   : totalPrice <= 6.5 * copiesNeeded
6.         <= 10000 : totalPrice <= 5.7 * copiesNeeded
7.         > 10000  : totalPrice <= 5 * copiesNeeded
8.     End Case
9.     Output('The total price is ' totalPrice)
10. End

Python Version

copiesNeeded = int(input("How many copies do you need? "))
totalPrice = None
if copiesNeeded < 1000:
	totalPrice = 6.5 * copiesNeeded
elif copiesNeeded <= 10000:
	totalPrice = 5.7 * copiesNeeded
else:
	totalPrice = 5 * copiesNeeded
print('The total price is {}'.format(totalPrice))

Problem 2

1. Begin
2.     chosenComment are int
3.     chosenComment = Random(1,6)
4.     Case chosenComment of
5.         1 : Output('The number was one!')
6.         2 : Output('The number was two!')
7.         3 : Output('The number was three!')
8.         4 : Output('The number was four!')
9.         5 : Output('The number was five!')
10.        6 : Output('The number was six!')
11.    End Case
12.End

Python Version

import random
chosenComment = random.randint(1,6)
match chosenComment:
	case 1:
		print('The number was one!')
	case 2:
		print('The number was two!')
	case 3:
		print('The number was three!')
	case 4:
		print('The number was four!')
	case 5:
		print('The number was five!')
	case 6:
		print('The number was six!')

Problem 3

1. Begin
2.     income, taxPayable are float
3.     Input(income)
4.     Case income of
5.         < 200    : taxPayable = 0
6.         <= 900   : taxPayable = (income - 100) * 0.28
7.         <= 1800  : taxPayable = 112 + (income - 500) * 0.32
8.         <= 3000  : taxPayable = 196 + (income - 1000) * 0.46
9.         > 3000   : taxPayable = 656 + (income - 2000) * 0.60
10.     End Case
11.     Output('The payable tax is ' income)
12. End

Python Version

income = float(input("Enter your weekly income "))
taxPayable = None
if income < 200:
	taxPayable = 0
elif income <= 900:
	taxPayable = 0.28 * (income - 100)
elif income <= 1800:
	taxPayable = 112 + 0.32 * (income - 500)
elif income <= 3000:
	taxPayable = 196 + 0.46 * (income - 1000)
else:
	taxPayable = 656 + 0.60 * (income - 2000)

For Next

Problem 1

1. Begin
2.     For Number <= 1 to 100
3.         Print(Number)
4.     Next
5. End

Python Version

[print(x+1) for x in range(100)]

Problem 2

1. Begin
2.     rainfall is float
3.     For Day <= 1 to 7
4.         Input(day_rainfall)
5.         rainfall <= rainfall + day_rainfall
6.     Next
7.     Output(rainfall)
8. End

Python Version

print(sum([int(input("Day's rainfall ")) for x in range(7)]))

Problem 3

1. Begin
2.     For Number <= 0 to 20
3.         Print(Number * 9 / 5 + 32)
4.     Next
5. End

Python Version

[print(x * 9 / 5 + 32) for x in range(21)]

Problem 4

1. Begin
2.     For Number <= 2 to 10
3.         Total = 1
4.         For FactTemp <= 1 to Number
5.             Total <= Total * FactTemp
6.         Next
7.         Print(Total)
8.     Next
9. End

Python Version

from functools import reduce
[print(reduce(lambda a,b: a * b, [y+1 for y in range(x+1)])) for x in range(1,10)]

While

Problem 1

1. Begin
2.     total, last_input is float
3.     last_input <= 0
4.     While last_input != -1
5.         total <= total + last_input
6.         Input(last_input)
7.     End While
8.     Output('Pretax total is ' total)
9.     Output('Amount of tax is ' total * 0.125)
10.    Output('Final total is ' total * 1.125)
11.End

Python Version

total = 0
last_input = 0
while last_input != -1:
	total = total + last_input
	last_input = int(input("Enter a number "))
print('Pretax total is {}'.format(total))
print('Amount of tax is {}'.format(total * 0.125))
print('Final total is {}'.format(total * 1.125))

Problem 2

1. Begin
2.     total, last_input, count is float
3.     last_input <= 0
4.     count <= -1
5.     While last_input != -1
6.         total <= total + last_input
7.         Input(last_input)
8.         count <= count + 1
9.     End While
10.    Output('The average is ' total / count)
11.End

Python Version

total = 0
last_input = 0
count = -1
while last_input != -1:
	total = total + last_input
	last_input = int(input("Enter a number "))
	count = count + 1;
print('The average is {}'.format(total / count))

Problem 3

1. Begin
2.     number, last_input, count is float
3.     number <= Random(1,100)
4.     last_input <= 0
5.     count <= 1
6.     Input(last_input)
7.     While last_input != number
9.         Input(last_input)
10.        count <= count + 1
11.    End While
12.    Output('It took you ' count ' tries!!')
13.End

Python Version

from random import randint
number = randint(1,3)
last_input = 0
count = 1
while (last_input := int(input("Guess: "))) != number:
	count = count + 1
print("It took you {} tries to guess!".format(count))

Function

1. Begin
2.     Function Factorial (n)
3.         If (n < 2) Then
4.             Return 1 // any number less than two has a factorial of 1
5.         Else
6.             Return n * Factorial(n-1) // any number >= 2 has a factorial of n * the factorial of n-1
7.         End If
8.     End Factorial
9.     Factorial(7) // call the function
10.End

Python Version


home