year_input = input("Enter the number representing your year (1-Freshmen, 2-Sophomore, 3-Junior, 4-Senior): ")
try:
year_input = int(year_input)
if year_input in year_levels:
print(f"You are a {year_levels[year_input]}!")
else:
print("Invalid input. Please enter a number from 1 to 4.")
except ValueError:
print("Invalid input. Please enter a number.")
Sure! I can help you with that. Here's a simple Python program that determines the year level of a student based on user input:
Python:
def determine_year_level():
try:
# Prompt the user to enter a number from 1 to 4
grade = int(input("Enter the grade (1-4): "))
# Determine the year level based on the input
if grade == 1:
year_level = "Freshmen"
elif grade == 2:
year_level = "Sophomore"
elif grade == 3:
year_level = "Junior"
elif grade == 4:
year_level = "Senior"
else:
year_level = "Invalid input"
# Print the year level
print("Year level:", year_level)
except ValueError:
print("Invalid input. Please enter a number.")
# Call the function to execute the program
determine_year_level()
In this program, we define a function called determine_year_level that prompts the user to enter a grade as an input. It then uses conditional statements (if, elif, else) to determine the corresponding year level based on the input. Finally, it prints the determined year level.
Note: The program will handle invalid inputs by displaying an error message when the user enters a non-numeric value.
I hope this helps! Let me know if you have any further questions.
year_input = input("Enter the number representing your year (1-Freshmen, 2-Sophomore, 3-Junior, 4-Senior): ")
try:
year_input = int(year_input)
if year_input in year_levels:
print(f"You are a {year_levels[year_input]}!")
else:
print("Invalid input. Please enter a number from 1 to 4.")
except ValueError:
print("Invalid input. Please enter a number.")
year_input = input("Enter the number representing your year (1-Freshmen, 2-Sophomore, 3-Junior, 4-Senior): ")
try:
year_input = int(year_input)
if year_input in year_levels:
print(f"You are a {year_levels[year_input]}!")
else:
print("Invalid input. Please enter a number from 1 to 4.")
except ValueError:
print("Invalid input. Please enter a number.")
year_input = input("Enter the number representing your year (1-Freshmen, 2-Sophomore, 3-Junior, 4-Senior): ")
try:
year_input = int(year_input)
if year_input in year_levels:
print(f"You are a {year_levels[year_input]}!")
else:
print("Invalid input. Please enter a number from 1 to 4.")
except ValueError:
print("Invalid input. Please enter a number.")