如何为Python版Rock Paper Scissors(石头剪刀布)游戏添加无错误的while循环
Hey there! Let's fix that looping issue for your Rock Paper Scissors game—you're already halfway there, just a few tweaks needed. Let's break down what went wrong and how to make it work smoothly.
First, let's address the two main issues you might have run into:
- Incorrect input validation logic: Your original
if myItem != "Rock" or myItem != "Paper" or myItem != "Scissors"condition is alwaysTrue(because any input will always not match at least two of the options). That's why you probably saw that error message even when entering valid choices. - Missing loop structure & exit condition: When wrapping code in a
whileloop, you need to make sure all repeatable logic is inside the loop, and give users a way to exit instead of running forever.
Here's the revised code with a working loop, plus some quality-of-life improvements for a better user experience:
import random print("=====Rock Paper Scissors: The Python Game=====\n") name = input("Player please enter your name:\n") while True: print("\n") # Handle user input with cleanup and exit option myItem = input("Enter Either Rock, Paper or Scissors (or 'quit' to exit):\n").strip().capitalize() # Exit the game if user types 'quit' if myItem == "Quit": print(f"Thanks for playing, {name}! See you next time!") break # Validate input is one of the valid choices if myItem not in ["Rock", "Paper", "Scissors"]: print("Whoops! That's not a valid choice. Please enter Rock, Paper, or Scissors.") continue # Skip the rest of the loop and ask for input again # Computer's choice (simplified with elif) computer_num = random.randint(1,3) if computer_num == 1: computerItem = "Rock" elif computer_num == 2: computerItem = "Paper" else: computerItem = "Scissors" print(f"Computer chooses {computerItem}...") # Determine the outcome if myItem == computerItem: print("Try Again!! You and the CPU picked the same thing") # Check if CPU wins elif (myItem == "Rock" and computerItem == "Paper") or \ (myItem == "Paper" and computerItem == "Scissors") or \ (myItem == "Scissors" and computerItem == "Rock"): # Print the winning logic if myItem == "Rock" and computerItem == "Paper": print("[Paper covers Rock]") elif myItem == "Paper" and computerItem == "Scissors": print("[Scissors cut Paper]") else: print("[Rock Smashes Scissors]") print("CPU WINS!!!") # If none of the above, user wins else: if myItem == "Paper" and computerItem == "Rock": print("[Paper covers Rock]") elif myItem == "Rock" and computerItem == "Scissors": print("[Rock smashes Scissors]") else: print("[Scissors cut Paper]") print(f"{name} WINS!!!")
Let's go over the key changes:
- Infinite loop with exit:
while Trueruns the game repeatedly until the user types "quit", which triggers abreakto exit the loop. - Better input handling:
strip().capitalize()fixes cases where users type lowercase, add spaces, etc. Thecontinuekeyword skips the rest of the loop if input is invalid, so the game asks for a new choice right away. - Simplified computer choice: Swapped three separate
ifstatements forif-elif-elseto make the code cleaner. - Streamlined outcome checks: Grouped all CPU-winning conditions into one
elifblock to reduce repetition, making the code easier to read and maintain.
This should let you play as many rounds as you want, with proper input validation and a clean way to exit the game.
内容的提问来源于stack exchange,提问作者IKUBIE NEMIEBOKA
相关产品推荐
相关产品推荐

