Skip to content

Commit

Permalink
Merge pull request #500 from realpython/python-keyboard-input
Browse files Browse the repository at this point in the history
Materials for user input tutorial
  • Loading branch information
KateFinegan authored Feb 20, 2024
2 parents 1bdca36 + 3f52a42 commit d090c06
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python-keyboard-input/README.md
Original file line number Diff line number Diff line change
@@ -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/).
25 changes: 25 additions & 0 deletions python-keyboard-input/banking_app.py
Original file line number Diff line number Diff line change
@@ -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!")
4 changes: 4 additions & 0 deletions python-keyboard-input/inputplus.py
Original file line number Diff line number Diff line change
@@ -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}")
8 changes: 8 additions & 0 deletions python-keyboard-input/integer_input.py
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 4 additions & 0 deletions python-keyboard-input/list_input.py
Original file line number Diff line number Diff line change
@@ -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}")
19 changes: 19 additions & 0 deletions python-keyboard-input/multiple_inputs.py
Original file line number Diff line number Diff line change
@@ -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)
)
27 changes: 27 additions & 0 deletions python-keyboard-input/quiz.py
Original file line number Diff line number Diff line change
@@ -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)}")
21 changes: 21 additions & 0 deletions python-keyboard-input/sensitive.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d090c06

Please sign in to comment.