Looping
Rather than setting a done
flag and looping while not(done):
(note: this would usually be written as simply while not done:
), you could use a formulation more like:
while True:
# do things
if loop_should_end:
break
This removes the need for the flag and makes the loop easier to follow.
Logic
At present, comp
is set outside the loop, so the computer will always guess the same thing. This makes it rather too easy to win - you should move comp = random.randint(1, 4)
(note whitespace) inside the loop.
Comparisons
The most obvious tweak is that the canonical replacement for lots of elif
s in Python is a dictionary. In this case, you could look up a tuple (comp, guess)
and print the result:
RESULTS = {
(1, 3): "Rock beats scissors, too bad!",
(2, 1): "Paper beats rock, tough luck!",
...
}
then access this inside the loop as easily as:
print(RESULTS.get((comp, guess), "It's a draw!"))
You could also simplify:
stop = input("Do you want to quit? ")
if stop.lower() == "y" or stop.lower() == "yes":
to:
if input("Do you want to quit? ").lower() in {"y", "yes"}:
Validation
You currently have no input validation, so the program could crash with a ValueError
if the user types 'scissors'
instead of 3
. See e.g. "Asking the user for input until they give a valid response" "Asking the user for input until they give a valid response" for guidance on this.
Structure
Finally, it is bad practice to have all of your code running at the top level of the script - it makes it much more difficult to re-use later. Instead, I would do something like:
import random
RESULTS = {...} # possible outcomes
def get_int_input(...):
"""Take valid integer input from user."""
...
def game_loop(...):
"""Main game loop."""
...
if __name__ == '__main__':
game_loop()
This will work exactly the same if you run the script directly (see "What does if __name__ == "__main__":
do?" "What does if __name__ == "__main__":
do?") but allows you to use e.g. from wherever import get_input_int
elsewhere, simplifying reuse and encouraging modular design.
Looping
Rather than setting a done
flag and looping while not(done):
(note: this would usually be written as simply while not done:
), you could use a formulation more like:
while True:
# do things
if loop_should_end:
break
This removes the need for the flag and makes the loop easier to follow.
Logic
At present, comp
is set outside the loop, so the computer will always guess the same thing. This makes it rather too easy to win - you should move comp = random.randint(1, 4)
(note whitespace) inside the loop.
Comparisons
The most obvious tweak is that the canonical replacement for lots of elif
s in Python is a dictionary. In this case, you could look up a tuple (comp, guess)
and print the result:
RESULTS = {
(1, 3): "Rock beats scissors, too bad!",
(2, 1): "Paper beats rock, tough luck!",
...
}
then access this inside the loop as easily as:
print(RESULTS.get((comp, guess), "It's a draw!"))
You could also simplify:
stop = input("Do you want to quit? ")
if stop.lower() == "y" or stop.lower() == "yes":
to:
if input("Do you want to quit? ").lower() in {"y", "yes"}:
Validation
You currently have no input validation, so the program could crash with a ValueError
if the user types 'scissors'
instead of 3
. See e.g. "Asking the user for input until they give a valid response" for guidance on this.
Structure
Finally, it is bad practice to have all of your code running at the top level of the script - it makes it much more difficult to re-use later. Instead, I would do something like:
import random
RESULTS = {...} # possible outcomes
def get_int_input(...):
"""Take valid integer input from user."""
...
def game_loop(...):
"""Main game loop."""
...
if __name__ == '__main__':
game_loop()
This will work exactly the same if you run the script directly (see "What does if __name__ == "__main__":
do?") but allows you to use e.g. from wherever import get_input_int
elsewhere, simplifying reuse and encouraging modular design.
Looping
Rather than setting a done
flag and looping while not(done):
(note: this would usually be written as simply while not done:
), you could use a formulation more like:
while True:
# do things
if loop_should_end:
break
This removes the need for the flag and makes the loop easier to follow.
Logic
At present, comp
is set outside the loop, so the computer will always guess the same thing. This makes it rather too easy to win - you should move comp = random.randint(1, 4)
(note whitespace) inside the loop.
Comparisons
The most obvious tweak is that the canonical replacement for lots of elif
s in Python is a dictionary. In this case, you could look up a tuple (comp, guess)
and print the result:
RESULTS = {
(1, 3): "Rock beats scissors, too bad!",
(2, 1): "Paper beats rock, tough luck!",
...
}
then access this inside the loop as easily as:
print(RESULTS.get((comp, guess), "It's a draw!"))
You could also simplify:
stop = input("Do you want to quit? ")
if stop.lower() == "y" or stop.lower() == "yes":
to:
if input("Do you want to quit? ").lower() in {"y", "yes"}:
Validation
You currently have no input validation, so the program could crash with a ValueError
if the user types 'scissors'
instead of 3
. See e.g. "Asking the user for input until they give a valid response" for guidance on this.
Structure
Finally, it is bad practice to have all of your code running at the top level of the script - it makes it much more difficult to re-use later. Instead, I would do something like:
import random
RESULTS = {...} # possible outcomes
def get_int_input(...):
"""Take valid integer input from user."""
...
def game_loop(...):
"""Main game loop."""
...
if __name__ == '__main__':
game_loop()
This will work exactly the same if you run the script directly (see "What does if __name__ == "__main__":
do?") but allows you to use e.g. from wherever import get_input_int
elsewhere, simplifying reuse and encouraging modular design.
Looping
Rather than setting a done
flag and looping while not(done):
(note: this would usually be written as simply while not done:
), you could use a formulation more like:
while True:
# do things
if loop_should_end:
break
This removes the need for the flag and makes the loop easier to follow.
Logic
At present, comp
is set outside the loop, so the computer will always guess the same thing. This makes it rather too easy to win - you should move comp = random.randint(1, 4)
(note whitespace) inside the loop.
Comparisons
The most obvious tweak is that the canonical replacement for lots of elif
s in Python is a dictionary. In this case, you could look up a tuple (comp, guess)
and print the result:
RESULTS = {
(1, 3): "Rock beats scissors, too bad!",
(2, 1): "Paper beats rock, tough luck!",
...
}
then access this inside the loop as easily as:
print(RESULTS.get((comp, guess), "It's a draw!"))
You could also simplify:
stop = input("Do you want to quit? ")
if stop.lower() == "y" or stop.lower() == "yes":
to:
if stopinput("Do you want to quit? ").lower() in {"y", "yes"}:
Validation
You currently have no input validation, so the program could crash with a ValueError
if the user types 'scissors'
instead of 3
. See e.g. "Asking the user for input until they give a valid response" for guidance on this.
Structure
Finally, it is bad practice to have all of your code running at the top level of the script - it makes it much more difficult to re-use later. Instead, I would do something like:
import random
RESULTS = {...} # possible outcomes
def get_int_input(...):
"""Take valid integer input from user."""
...
def game_loop(...):
"""Main game loop."""
...
if __name__ == '__main__':
game_loop()
This will work exactly the same if you run the script directly (see "What does if __name__ == "__main__":
do?") but allows you to use e.g. from wherever import get_input_int
elsewhere, simplifying reuse and encouraging modular design.
Looping
Rather than setting a done
flag and looping while not(done):
(note: this would usually be written as simply while not done:
), you could use a formulation more like:
while True:
# do things
if loop_should_end:
break
This removes the need for the flag and makes the loop easier to follow.
Logic
At present, comp
is set outside the loop, so the computer will always guess the same thing. This makes it rather too easy to win - you should move comp = random.randint(1, 4)
(note whitespace) inside the loop.
Comparisons
The most obvious tweak is that the canonical replacement for lots of elif
s in Python is a dictionary. In this case, you could look up a tuple (comp, guess)
and print the result:
RESULTS = {
(1, 3): "Rock beats scissors, too bad!",
(2, 1): "Paper beats rock, tough luck!",
...
}
then access this inside the loop as easily as:
print(RESULTS.get((comp, guess), "It's a draw!"))
You could also simplify:
if stop.lower() == "y" or stop.lower() == "yes":
to:
if stop.lower() in {"y", "yes"}:
Validation
You currently have no input validation, so the program could crash with a ValueError
if the user types 'scissors'
instead of 3
. See e.g. "Asking the user for input until they give a valid response" for guidance on this.
Structure
Finally, it is bad practice to have all of your code running at the top level of the script - it makes it much more difficult to re-use later. Instead, I would do something like:
import random
RESULTS = {...} # possible outcomes
def get_int_input(...):
"""Take valid integer input from user."""
...
def game_loop(...):
"""Main game loop."""
...
if __name__ == '__main__':
game_loop()
This will work exactly the same if you run the script directly (see "What does if __name__ == "__main__":
do?") but allows you to use e.g. from wherever import get_input_int
elsewhere, simplifying reuse and encouraging modular design.
Looping
Rather than setting a done
flag and looping while not(done):
(note: this would usually be written as simply while not done:
), you could use a formulation more like:
while True:
# do things
if loop_should_end:
break
This removes the need for the flag and makes the loop easier to follow.
Logic
At present, comp
is set outside the loop, so the computer will always guess the same thing. This makes it rather too easy to win - you should move comp = random.randint(1, 4)
(note whitespace) inside the loop.
Comparisons
The most obvious tweak is that the canonical replacement for lots of elif
s in Python is a dictionary. In this case, you could look up a tuple (comp, guess)
and print the result:
RESULTS = {
(1, 3): "Rock beats scissors, too bad!",
(2, 1): "Paper beats rock, tough luck!",
...
}
then access this inside the loop as easily as:
print(RESULTS.get((comp, guess), "It's a draw!"))
You could also simplify:
stop = input("Do you want to quit? ")
if stop.lower() == "y" or stop.lower() == "yes":
to:
if input("Do you want to quit? ").lower() in {"y", "yes"}:
Validation
You currently have no input validation, so the program could crash with a ValueError
if the user types 'scissors'
instead of 3
. See e.g. "Asking the user for input until they give a valid response" for guidance on this.
Structure
Finally, it is bad practice to have all of your code running at the top level of the script - it makes it much more difficult to re-use later. Instead, I would do something like:
import random
RESULTS = {...} # possible outcomes
def get_int_input(...):
"""Take valid integer input from user."""
...
def game_loop(...):
"""Main game loop."""
...
if __name__ == '__main__':
game_loop()
This will work exactly the same if you run the script directly (see "What does if __name__ == "__main__":
do?") but allows you to use e.g. from wherever import get_input_int
elsewhere, simplifying reuse and encouraging modular design.
Looping
Rather than setting a done
flag and looping while not(done):
(note: this would usually be written as simply while not done:
), you could use a formulation more like:
while True:
# do things
if loop_should_end:
break
This removes the need for the flag and makes the loop easier to follow.
Logic
At present, comp
is set outside the loop, so the computer will always guess the same thing. This makes it rather too easy to win - you should move comp = random.randint(1, 4)
(note whitespace) inside the loop.
Comparisons
The most obvious tweak is that the canonical replacement for lots of elif
s in Python is a dictionary. In this case, you could look up a tuple (comp, guess)
and print the result:
RESULTS = {
(1, 3): "Rock beats scissors, too bad!",
(2, 1): "Paper beats rock, tough luck!",
...
}
then access this inside the loop as easily as:
print(RESULTS.get((comp, guess), "It's a draw!"))
You could also simplify:
if stop.lower() == "y" or stop.lower() == "yes":
to:
if stop.lower() in {"y", "yes"}:
Validation
You currently have no input validation, so the program could crash with a ValueError
if the user types 'scissors'
instead of 3
. See e.g. "Asking the user for input until they give a valid response" for guidance on this.
Structure
Finally, it is bad practice to have all of your code running at the top level of the script - it makes it much more difficult to re-use later. Instead, I would do something like:
import random
RESULTS = {...} # possible outcomes
def get_int_input(...):
"""Take valid integer input from user."""
...
def game_loop(...):
"""Main game loop."""
...
if __name__ == '__main__':
game_loop()
This will work exactly the same if you run the script directly (see "What does if __name__ == "__main__":
do?") but allows you to use e.g. from wherever import get_input_int
elsewhere, simplifying reuse and encouraging modular design.