We learned about for loops, while loops, do/while loops, and infinite loops. We also learned try and except. The hacks and homework helped me understand what type of loop to use, and how they would operate.
#Popcorn1
stu = input("Are you are student? Y/N ")
age = int(input("What is your age? "))
tix = 10
if stu == "Y":
print("Students get a 10% discount")
if 0 <= age <= 12:
tix = tix * 0.5 * 0.2
elif 12 <= age <= 63:
tix = tix * 0.2
elif age >= 65:
tix = tix * 0.3 * 0.2
else:
print("Invalid age")
elif stu == "N":
if 0 <= age <= 12:
tix = tix * 0.5
elif 12 <= age <= 63:
tix = tix
elif age >= 65:
tix = tix * 0.3
else:
print("Invalid age")
else:
print("Please try again")
print("Your ticket will cost", tix, "dollars. ")
#Popcorn2
try:
num = int(input("Enter a number: ")) # Code that might raise an exception
result = 10 / num # Could raise ZeroDivisionError if num is 0
print("Result:", result)
if num % 2 != 0:
print("odd")
else:
print("even")
except ValueError:
print("That's not a valid number!")
'''
Create a program that asks the user for a number and keeps asking until they enter a positive number.
Requirements:
Input Validation: Make sure that the input is actually a number. Display an ‘error’ message if the user inputted something that is NOT a number
Positive Checker: If the user has entered a number that is positive, print a ‘success!’ message with the entered number
If the user has entered a number that is not positive like zero or a negative number, print a ‘try again’ message, prompting them to keep inputting numbers till they input a positive number.
'''
x = False
while x == False:
try:
num = int(input("Enter a number: ")) # Code that might raise an exception
if num > 0:
print("success")
x = True
else:
print("try again")
except ValueError:
print("error")