-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into duck-typing-python
- Loading branch information
Showing
8 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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
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/). |
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
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!") |
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
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}") |
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
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") |
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
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}") |
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
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) | ||
) |
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
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)}") |
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
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() |