Last active
February 13, 2021 04:36
-
-
Save mreed4/e5e6346f72de862e37dc685f3690f13f to your computer and use it in GitHub Desktop.
Exercise 1 from PracticePython.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.practicepython.org/exercise/2014/01/29/01-character-input.html | |
# Create a program that asks the user to enter their name and their age. Print out a message addressed to them that | |
# tells them the year that they will turn 100 years old. | |
# Get current year | |
import datetime | |
date = datetime.datetime.now() | |
year = date.year | |
# Greet user | |
print("\nWelcome, user.\n") | |
# User input | |
def func_user_input(): | |
# Get name | |
get_name = str(input("What is your name? ")) | |
name = get_name | |
# Get age | |
get_age = int(input("How old are you? ")) | |
age = get_age | |
# Display name and year that the user will turn 100 | |
message = f"\nHello {name}! You will turn 100 years of age no later than the year {(100 - age) + year}." | |
print(message) | |
# Get user-defined number | |
def func_get_number(): | |
get_number = int(input(f"\nNext, please enter a random number from 1-10: ")) | |
number = get_number | |
# Weed out numbers larger than 10 or smaller than 1 | |
if number > 10: | |
print(f"\nError: {number} is too large, try again!") | |
func_get_number() | |
elif number < 1: | |
print(f"\nError: {number} is too small, try again!") | |
func_get_number() | |
else: | |
print(f"\nPrinting last message {number} times") | |
print(number * message + "\n") | |
def func_get_choice(): | |
get_choice = str(input("Run script again? (Y/N) ")) | |
# Ensure input is either Y or N | |
if get_choice == "Y" or get_choice == "y": | |
print("\nRestarting script ...\n") | |
func_user_input() | |
elif get_choice == "N" or get_choice == "n": | |
return | |
else: | |
print(f"\nPlease enter \"Y\" or \"N\"!\n") | |
func_get_choice() | |
func_get_choice() | |
func_get_number() | |
func_user_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment