diff --git a/python-keyboard-input/README.md b/python-keyboard-input/README.md new file mode 100644 index 0000000000..faa479a844 --- /dev/null +++ b/python-keyboard-input/README.md @@ -0,0 +1,3 @@ +# How to Read User Input From the Keyboard in Python + +This folder provides the code examples for the Real Python tutorial [How to Read User Input From the Keyboard in Python](https://realpython.com/python-keyboard-input/). diff --git a/python-keyboard-input/banking_app.py b/python-keyboard-input/banking_app.py new file mode 100644 index 0000000000..27b712e20c --- /dev/null +++ b/python-keyboard-input/banking_app.py @@ -0,0 +1,25 @@ +import pyinputplus as pyip + +account_balance = 1000 + +print("Welcome to REALBank.") +while True: + print(f"\nYour account balance: ${account_balance}") + transaction_type = pyip.inputChoice(["Deposit", "Withdraw", "Exit"]) + + if transaction_type == "Exit": + break + elif transaction_type == "Deposit": + deposit_amount = pyip.inputInt( + prompt="Enter amount (max $10,000): $", min=0, max=10000 + ) + account_balance += deposit_amount + print(f"Deposited ${deposit_amount}.") + elif transaction_type == "Withdraw": + withdrawal_amount = pyip.inputInt( + prompt="Enter amount: $", min=0, max=account_balance + ) + account_balance -= withdrawal_amount + print(f"Withdrew ${withdrawal_amount}.") + +print("\nThank you for choosing REALBank. We hope to see you again soon!") diff --git a/python-keyboard-input/inputplus.py b/python-keyboard-input/inputplus.py new file mode 100644 index 0000000000..5b97cb5933 --- /dev/null +++ b/python-keyboard-input/inputplus.py @@ -0,0 +1,4 @@ +import pyinputplus as pyip + +age = pyip.inputInt(prompt="Enter your age: ", min=0, max=120) +print(f"Your age is: {age}") diff --git a/python-keyboard-input/integer_input.py b/python-keyboard-input/integer_input.py new file mode 100644 index 0000000000..4a38a45f4d --- /dev/null +++ b/python-keyboard-input/integer_input.py @@ -0,0 +1,8 @@ +while True: + try: + age = int(input("How old are you? ")) + except ValueError: + print("Please enter a number for your age.") + else: + break +print(f"Next year, you'll be {age + 1} years old") diff --git a/python-keyboard-input/list_input.py b/python-keyboard-input/list_input.py new file mode 100644 index 0000000000..68b7c7853c --- /dev/null +++ b/python-keyboard-input/list_input.py @@ -0,0 +1,4 @@ +user_colors = input("Enter the three secondary colors separated by commas: ") +colors = [s.strip() for s in user_colors.split(",")] + +print(f"List of colors: {colors}") diff --git a/python-keyboard-input/multiple_inputs.py b/python-keyboard-input/multiple_inputs.py new file mode 100644 index 0000000000..0c04e823f9 --- /dev/null +++ b/python-keyboard-input/multiple_inputs.py @@ -0,0 +1,19 @@ +print("Name at least 3 colors in the Kenyan flag.") + +kenya_flag_colors = {"green", "black", "red", "white"} +user_colors = set() + +while len(user_colors) < 3: + color = input("Enter a color on the Kenya flag: ") + user_colors.add(color.lower()) + +if user_colors.issubset(kenya_flag_colors): + print( + "Correct! These colors are all in the Kenyan flag: " + + ", ".join(user_colors) + ) +else: + print( + "Incorrect. The colors of the Kenyan flag are: " + + ", ".join(kenya_flag_colors) + ) diff --git a/python-keyboard-input/quiz.py b/python-keyboard-input/quiz.py new file mode 100644 index 0000000000..d46dbc67ad --- /dev/null +++ b/python-keyboard-input/quiz.py @@ -0,0 +1,27 @@ +questions = { + "What are the four colors of the Kenyan flag? ": { + "green", + "black", + "red", + "white", + }, + "What are the three colors of the French flag? ": {"blue", "red", "white"}, + "How do you spell the first three numbers in Norwegian? ": { + "en", + "to", + "tre", + }, +} + +for question, correct in questions.items(): + while True: + answers = { + answer.strip().lower() for answer in input(question).split(",") + } + if len(answers) == len(correct): + break + print(f"Please enter {len(correct)} answers separated by comma") + if answers == correct: + print("Correct") + else: + print(f"No, the correct answer is {', '.join(correct)}") diff --git a/python-keyboard-input/sensitive.py b/python-keyboard-input/sensitive.py new file mode 100644 index 0000000000..6b8aa2b5a0 --- /dev/null +++ b/python-keyboard-input/sensitive.py @@ -0,0 +1,21 @@ +import os +import getpass + + +def verify_email(email): + allowed_emails = [ + email.strip() for email in os.getenv("ALLOWED_EMAILS").split(",") + ] + return email in allowed_emails + + +def main(): + email = getpass.getpass("Enter your email address: ") + if verify_email(email): + print("Email is valid. You can proceed.") + else: + print("Incorrect email. Access denied.") + + +if __name__ == "__main__": + main()