In this lesson, we learned about nested loops, and how they are helpful when having multiple conditions that need to be met.
Homework (Hacks)
- Write pseudocode to determine if a student passes a class based on their exam scores and attendance using nested conditionals.
- Write a python segment to decide the shipping cost based on the weight of a package and the delivery speed chosen (standard or express) using nested conditionals.
- Write a python segment to have different ticket prices for different ages, with a discount for students
Challenge Hack
- Write a program that helps a user determine the type of triangle based on the lengths of its sides. The program should prompt the user to input three positive numbers representing the sides of a triangle. Your task is to use nested conditionals to check the following:
- First, verify if the three sides can form a valid triangle (hint: the sum of any two sides must be greater than the third side). If it’s a valid triangle, further classify it into one of the following categories:
- Equilateral Triangle: All sides are equal.
- Isosceles Triangle: Two sides are equal.
- Scalene Triangle: No sides are equal. If the sides do not form a valid triangle, the program should display an appropriate message.
- Requirements: Use nested conditionals to handle the logic for checking the type of triangle. Include input validation to ensure the user enters positive numbers
#HOMEWORKHACK
'''
Pseudocode:
exam = int(input("Please enter your number grade for this trimester: "))
attend = int(input("Please enter the number of days you missed: "))
days = int(input("Please enter the length of the school year: "))
print("Passing grade is 70% or above. If absences account for more that 50% of the school year, it is an automatic fail. ")
if attend/days >= 0.5:
print("You missed more than half of the school year, and will have to repeat the class.")
else:
if exam >= 70:
print("Congrats! You've passed this class.")
else:
print("You need to retake this class. ")
'''
#1
price = None
weight = int(input("How much does your package weight? "))
lbs = str(input("pounds or kilograms "))
if lbs == "kilograms":
weight = weight * 2.20462
price = weight * 2
print("Express takes 2-4 days, standard takes 8-10 days.")
ship = input("Do you want express or standard shipping? ").lower()
if ship == "express":
price = price + price * 0.2
elif ship == "standard":
price = price
else:
print("INVALID")
print("Shipping will cost $" + str(price) + ".")
#2
#from tabulate import tabulate as tab
#table = [['','5 and below', '12-18', '18-65', '65+'], ['1 tix', 1, 3, 5, 3], ['2 tix', 1, 5, 9, 5]]
#print(tab(table, headers='firstrow', tablefmt='fancy_grid'))
print("Tickets are 2 dollars, 1 dollar for seniors.")
num = int(input("How many tickets are you buying? "))
#type = input("For what ages?").split([])
type = input("Age? ")
stu = input("Are you a student? Y/N ")
price = 0
'''
for i in range(0, len(type)):
if type[i] >= type[i+1]:
temp = type[i]
type[i] = type[i+1]
type[i+1] = temp
'''
if int(type) >= 65:
price = num
else:
price = num * 2
if stu == "Y":
price = price * 0.5
print(price)
#challange
side1 = abs(int(input("Please enter the length of 1 side: ")))
side2 = abs(int(input("The length of another side: ")))
side3 = abs(int(input("Length of last side: ")))
if side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1:
if side1 == side2 and side2 == side3:
print("EQUILATERAL")
elif side1 == side2 or side2 == side3 or side1 == side3:
print("ISOLCESES")
else:
print("SCALENE")
else:
print("Invalid triangle.")