def main(): random_number = random.randint(1, 9) user_guess = int(user_data_validation( input("Guess the number that the computer has generated between 1 and 9: "))) user_guess_not_equal_to_random_number = True number_of_guesses = 0 while user_guess > 9 or user_guess < 1: try: raise ValueError("Guess is out of bounds:") except ValueError as err: print(err) print("please choose a value between 1 and 9") user_guess = int(user_data_validation( input("Guess the number that the computer has generated between 1 and 9: "))) while user_guess_not_equal_to_random_number: if user_guess > random_number: print("Your guess is too high") number_of_guesses += 1 user_guess = int(user_data_validation( input("Guess the number that the computer has generated between 1 and 9: "))) elif user_guess < random_number: print("Your guess is too low") number_of_guesses += 1 user_guess = int(user_data_validation( input("Guess the number that the computer has generated between 1 and 9: "))) else: number_of_guesses += 1 print("your guess is correct! You took {} guesses".format(number_of_guesses)) user_guess_not_equal_to_random_number = False
def main(): name = user_data_validation(input('What is your name? '), 'You must specify a name to proceed:') age = int( user_data_validation( input('Hello {}. What is your age? '.format(name)), 'you must provide an age to proceed:')) birth_year = datetime.datetime.now().year - age hundredth_birthday = birth_year + 100 iterator = user_data_validation( input('Can you provide a value between 1 - 10?'), 'you must provide a valid integer to proceed:') while int(iterator) > 10: try: raise ValueError( 'I need a number between 1 and 10. Provide a value within that range:' ) except ValueError as err: print(err) iterator = user_data_validation( input(), 'you must provide a valid integer to proceed:') iterator = int(iterator) while iterator > 0: if age < 100: print('The year {} will turn 100 is {}'.format( name, hundredth_birthday)) else: length_of_time = datetime.datetime.now().year - hundredth_birthday print('{} turned 100 {} years ago, in {}'.format( name, length_of_time, hundredth_birthday)) iterator -= 1
def main(): is_prime_number() user_response = user_data_validation( input("would you like to see the calculations? (Y/N)"), "Please provide an answer: ").lower() if user_response == "y": print_calculations()
def create_user_generated_list(): user_generated_list = [] user_input_value = int( user_data_validation(input('Please provide a value: '))) for number in number_list: if number < user_input_value: user_generated_list.append(number) print('original list: {}'.format(number_list)) print('user generated list: {}'.format(user_generated_list))
def main(): user_input = (input('Please provide a number:')) user_input = user_data_validation(user_input) user_input = int(user_input) if (user_input % 4) == 0: print('This is divisible by 4') elif (user_input % 2) == 0: print('This is an even number') else: print('This is an odd number') num = int(user_data_validation(input('Provide another number:'))) check = int( user_data_validation( input('Provide another number to do a calculation: '))) remainder = num % check if remainder == 0: print('{} successfully divides into {}'.format(check, num)) else: print('There is {} remaining when dividing {} by {}'.format( remainder, check, num))
#!/usr/bin/python3 """ Ask the user for a number and determine whether the number is prime or not. (For those who have forgotten, a prime number is a number that has no divisors.). You can (and should!) use your answer to Exercise 4 to help you. Take this opportunity to practice using functions, described below. """ from helpers import user_data_validation # Globals user_generated_value = int( user_data_validation(input('Please provide a number: '))) calculations = [] def print_calculations(): for l in calculations: print("{} / {} = {}".format(l[0], l[1], l[2])) def is_prime_number(): divisor = 1 divisor_list = [] while divisor <= user_generated_value: if user_generated_value % divisor == 0: divisor_list.append(divisor) result = int(user_generated_value / divisor) calculations.append((user_generated_value, divisor, result)) divisor += 1