-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (66 loc) · 2.18 KB
/
Copy pathmain.py
File metadata and controls
84 lines (66 loc) · 2.18 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from dataclasses import dataclass
# Represents a dog with a name, weight, and whether it is tired.
@dataclass
class Dog:
name: str
weight: int
is_tired: bool
cleaned: bool = False
# Returns the sound the dog makes.
# If the dog is tired, it sleeps.
# If the dog weighs at least 50 pounds, it barks loudly.
# Otherwise, it barks quietly.
def dog_sound(dog: Dog) -> str:
if dog.is_tired:
return "zzzzz"
elif dog.weight >= 50:
return "RUFF RUFF"
else:
return "yip yip yip"
def get_dog_weight(dog: Dog) -> int:
return dog.weight
def clean_dog(dog: Dog) -> None:
dog.cleaned = True
return dog.cleaned
# Plays with the dog by making it tired.
def play_with_dog(dog: Dog) -> None:
dog.is_tired = True
# Lets the user choose which dog to play with.
def input_dog_choice(dog1: Dog, dog2: Dog, dog3: Dog) -> Dog | None:
while True:
print(
f"Play with [{dog1.name}], [{dog2.name}], [{dog3.name}], or [quit]?")
dog_choice = input("> ")
if dog_choice == "quit":
return None
elif dog_choice == dog1.name:
return dog1
elif dog_choice == dog2.name:
return dog2
elif dog_choice == dog3.name:
return dog3
else:
print("Please provide a valid dog name or quit.")
# Runs the main program.
def main() -> None:
# Create three Dog objects.
dog1 = Dog("Fido", 30, False)
dog2 = Dog("Rufus", 55, False)
dog3 = Dog("Big Stuff", 7, False)
# Continue until the user chooses to quit.
while True:
# Display the sound each dog is making.
print(f"{dog1.name} says {dog_sound(dog1)}")
print(f"{dog2.name} says {dog_sound(dog2)}")
print(f"{dog3.name} says {dog_sound(dog3)}")
# Ask the user which dog they want to play with.
dog_to_play_with = input_dog_choice(dog1, dog2, dog3)
# End the program if the user enters "quit".
if dog_to_play_with is None:
break
# Play with the selected dog.
print(f"Playing with {dog_to_play_with.name}")
play_with_dog(dog_to_play_with)
# Starts the program.
if __name__ == "__main__":
main()