Skip to content

Commit a8fa067

Browse files
committed
Added lesson 15
1 parent 3efd214 commit a8fa067

4 files changed

Lines changed: 144 additions & 3 deletions

File tree

β€Ž.gitignoreβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

β€ŽREADME.mdβ€Ž

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@
4646

4747
### πŸ“š References
4848
- πŸ”— [Python Official Site](https://www.python.org/)
49-
- πŸ”— [Python String Methods](https://docs.python.org/3/library/stdtypes.html#textseq)
50-
- πŸ”— [Python Math Module](https://docs.python.org/3/library/math.html?highlight=math#module-math)
51-
- πŸ”— [Python Random Module](https://docs.python.org/3/library/random.html?highlight=random%20module#module-random)
49+
- πŸ”— [Python Standard Library](https://docs.python.org/3/library/index.html)
50+
- πŸ”— [Python Package Index](https://pypi.org/)
51+
- πŸ”— [Python string methods](https://docs.python.org/3/library/stdtypes.html#textseq)
52+
- πŸ”— [Python math module](https://docs.python.org/3/library/math.html)
53+
- πŸ”— [Python random module](https://docs.python.org/3/library/random.html)
54+
- πŸ”— [Python argparse module](https://docs.python.org/3/library/argparse.html)
5255

5356
---
5457

@@ -68,3 +71,4 @@
6871
- πŸ”— [Chapter 12 - Closures](https://github.com/gitdagray/python-course/tree/main/lesson12)
6972
- πŸ”— [Chapter 13 - f-Strings](https://github.com/gitdagray/python-course/tree/main/lesson13)
7073
- πŸ”— [Chapter 14 - Modules](https://github.com/gitdagray/python-course/tree/main/lesson14)
74+
- πŸ”— [Chapter 15 - Command Line Arguments](https://github.com/gitdagray/python-course/tree/main/lesson15)

β€Žlesson15/hello_person.pyβ€Ž

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def hello(name, lang):
2+
greetings = {
3+
"English": "Hello",
4+
"Spanish": "Hola",
5+
"German": "Hallo",
6+
}
7+
msg = f"{greetings[lang]} {name}!"
8+
print(msg)
9+
10+
11+
if __name__ == '__main__':
12+
import argparse
13+
14+
parser = argparse.ArgumentParser(
15+
description="Provides a personal greeting."
16+
)
17+
18+
parser.add_argument(
19+
"-n", "--name", metavar="name",
20+
required=True, help="The name of the person to greet."
21+
)
22+
23+
parser.add_argument(
24+
"-l", "--lang", metavar="language",
25+
required=True, choices=["English", "Spanish", "German"],
26+
help="The language of the greeting."
27+
)
28+
29+
args = parser.parse_args()
30+
31+
hello(args.name, args.lang)

β€Žlesson15/rps8.pyβ€Ž

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import sys
2+
import random
3+
from enum import Enum
4+
5+
6+
def rps(name='PlayerOne'):
7+
game_count = 0
8+
player_wins = 0
9+
python_wins = 0
10+
11+
def play_rps():
12+
nonlocal name
13+
nonlocal player_wins
14+
nonlocal python_wins
15+
16+
class RPS(Enum):
17+
ROCK = 1
18+
PAPER = 2
19+
SCISSORS = 3
20+
21+
playerchoice = input(
22+
f"\n{name}, please enter... \n1 for Rock,\n2 for Paper, or \n3 for Scissors:\n\n")
23+
24+
if playerchoice not in ["1", "2", "3"]:
25+
print(f"{name}, please enter 1, 2, or 3.")
26+
return play_rps()
27+
28+
player = int(playerchoice)
29+
30+
computerchoice = random.choice("123")
31+
32+
computer = int(computerchoice)
33+
34+
print(f"\n{name}, you chose {str(RPS(player)).replace('RPS.', '').title()}.")
35+
print(
36+
f"Python chose {str(RPS(computer)).replace('RPS.', '').title()}.\n"
37+
)
38+
39+
def decide_winner(player, computer):
40+
nonlocal name
41+
nonlocal player_wins
42+
nonlocal python_wins
43+
if player == 1 and computer == 3:
44+
player_wins += 1
45+
return f"πŸŽ‰ {name}, you win!"
46+
elif player == 2 and computer == 1:
47+
player_wins += 1
48+
return f"πŸŽ‰ {name}, you win!"
49+
elif player == 3 and computer == 2:
50+
player_wins += 1
51+
return f"πŸŽ‰ {name}, you win!"
52+
elif player == computer:
53+
return "😲 Tie game!"
54+
else:
55+
python_wins += 1
56+
return f"🐍 Python wins!\nSorry, {name}..😒"
57+
58+
game_result = decide_winner(player, computer)
59+
60+
print(game_result)
61+
62+
nonlocal game_count
63+
game_count += 1
64+
65+
print(f"\nGame count: {game_count}")
66+
print(f"\n{name}'s wins: {player_wins}")
67+
print(f"\nPython wins: {python_wins}")
68+
69+
print(f"\nPlay again, {name}?")
70+
71+
while True:
72+
playagain = input("\nY for Yes or \nQ to Quit\n")
73+
if playagain.lower() not in ["y", "q"]:
74+
continue
75+
else:
76+
break
77+
78+
if playagain.lower() == "y":
79+
return play_rps()
80+
else:
81+
print("\nπŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰")
82+
print("Thank you for playing!\n")
83+
sys.exit(f"Bye {name}! πŸ‘‹")
84+
85+
return play_rps
86+
87+
88+
89+
90+
if __name__ == "__main__":
91+
import argparse
92+
93+
parser = argparse.ArgumentParser(
94+
description="Provides a personalized game experience."
95+
)
96+
97+
parser.add_argument(
98+
"-n", "--name", metavar="name",
99+
required=True, help="The name of the person playing the game."
100+
)
101+
102+
args = parser.parse_args()
103+
104+
rock_paper_scissors = rps(args.name)
105+
rock_paper_scissors()

0 commit comments

Comments
Β (0)