-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (51 loc) · 1.7 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
winning_combinations = {
"rock": "scissors",
"scissors": "paper",
"paper": "rock",
}
scores = {
"player": 0,
"computer": 0,
}
def random_choice():
import random
return random.choice(list(winning_combinations.keys()))
def get_winner(name, player, computer):
if player == computer:
return "It's a tie!"
if winning_combinations[player] == computer:
scores["player"] += 1
return f"{name} wins! The score is {scores['player']} - {scores['computer']}"
scores["computer"] += 1
return f"Computer wins! The score is {scores['player']} - {scores['computer']}"
def invalid_input(input, type="choice"):
if type == "name":
return input == "" or input.isspace() or input.isdigit()
return input not in winning_combinations.keys()
def get_input(type):
data = input(f"Enter your {type}: ")
if invalid_input(data, type):
print("Invalid input. Try again.")
return get_input(type)
return data
def start_game():
print("Welcome to Rock, Paper, Scissors!")
print("You will be playing against the computer.")
print("Feel free to play for as many rounds as you'd like.")
print("Good luck!")
player_name = get_input("name")
play_game(player_name)
def play_game(player_name):
computer_input = random_choice()
player_input = get_input("choice").lower()
print(get_winner(player_name, player_input, computer_input))
print(determineNextGame(player_name))
def determineNextGame(player_name):
play_again = input(f"Do you want to play again {player_name}? (Y/N): ")
if play_again.upper() == "Y":
return play_game(player_name)
if play_again.upper() == "N":
return "Thanks for playing!"
print("Invalid input. Try again.")
determineNextGame(player_name)
start_game()