from verify_input import verify # Get user input first_number = input("Enter first number: ") second_number = input("Enter second number: ") # Check if both numbers are valid if verify([first_number, second_number], "int") and int(first_number) > 0 and int(second_number) > 0: # Convert string inputs to ints first_number, second_number = int(first_number), int(second_number) # Let d be the divisor / multiple # Set d initially to the smaller of the 2 numbers d = min(first_number, second_number) while first_number % d != 0 or second_number % d != 0: d -= 1 # Print the greatest common divisor print(d) else: print("Invalid input")
from verify_input import verify # Get user input number = input("Enter number: ") if verify(number, "int"): # Convert string input to integer number = int(number) # Check using remainder when number is divided by 2 if number % 2 == 1: print("{0} is odd".format(number)) else: print("{0} is even".format(number)) else: print("Invalid input")
from verify_input import verify # Import function to check leap year from q04_determine_leap_year import check_leap # Map month number to name month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] # Get user input month = input("Enter month: ") year = input("Enter year: ") if verify([month, year], "int"): # Convert string inputs to ints month, year = int(month), int(year) if month == 2: # Number of days in February is dependent on whether it is a leap year if check_leap(year): print("{0} {1} has 29 days".format(month_names[month - 1], year)) else: print("{0} {1} has 28 days".format(month_names[month - 1], year)) elif month in [4, 6, 9, 11]: print("{0} {1} has 30 days".format(month_names[month - 1], year)) else: print("{0} {1} has 31 days".format(month_names[month - 1], year)) else: print("Invalid input")
from verify_input import verify # Get user input score = input("Enter score: ") if verify(score, "float") and 0 <= (float(score)) <= 100: # Convert string input to float score = float(score) # Check grade if score >= 70: grade = "A" elif score >= 60: grade = "B" elif score >= 55: grade = "C" elif score >= 50: grade = "D" elif score >= 45: grade = "E" elif score >= 35: grade = "S" else: grade = "U" # Print grade print(grade) else: print("Invalid input")
from verify_input import verify def check_leap(year): # Return True if it is a leap year, False if it is not return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 # Code below is only executed if module is ran as a standalone program, not as an imported module if __name__ == "__main__": # Get user input year_input = input("Enter year: ") if verify(year_input, "int"): # Convert string input to int year_input = int(year_input) # Check and print result if check_leap(year_input): print("Leap") else: print("Non-leap") else: print("Invalid input")
from verify_input import verify # Get user input number_of_students = input("Enter the number of students: ") if verify(number_of_students, "int") and int(number_of_students) >= 2: # Convert string input to int number_of_students = int(number_of_students) # Store the strings for recursive input prompt1 = "Enter name of student {0}: " prompt2 = "Enter score of student {0}: " try: students = [ (input(prompt1.format(i + 1)), float(input(prompt2.format(i + 1)))) for i in range(number_of_students) ] # Sort the list by the second object of the tuples students = sorted(students, key=lambda x: x[1], reverse=True) # Print result print("Highest scorer is {0} with a score of {1}".format((students[0])[0], (students[0])[1])) print("Second highest scorer is {0} with a score of {1}".format((students[1])[0], (students[1])[1])) except ValueError: print("Invalid input") else: print("Invalid input")
from verify_input import verify # Enter lengths of 3 sides of triangle side = [input("Enter side {0}: ".format(i)) for i in range(3)] if verify(side, "float"): # Convert strings in list to arrays side = [float(i) for i in side] # Sort array according to magnitude of length side.sort() # Check if largest side is smaller than sum of the other 2 sides if side[2] < (side[0] + side[1]): # Calculate perimeter and print it print("Perimeter = {0}".format(sum(side))) else: print("Invalid triangle!") else: print("Invalid input")