Python 2石头剪刀布程序异常求助:输入后重复触发选择询问
Let's break down why your game gets stuck repeating the input prompt no matter what you enter — you're dealing with a mix of loop logic errors, Python 2-specific input handling mistakes, and variable mismatches. Here's a detailed breakdown and fixed code:
Key Problems Identified
1. Multiple Isolated while Loops
Your code has three separate while status=='yes' loops. The first loop will run infinitely because:
- It never modifies
statusto exit the loop - None of the battle conditions trigger (more on that below), so it just cycles back to asking for input
- The other two loops will never even run, since the first loop is blocking execution entirely
All game logic (input, computer choice, battle resolution, command handling) needs to live inside a single loop so each round flows sequentially.
2. Wrong Input Function for Python 2
In Python 2, input() evaluates user input as Python code. If you type rock, it tries to access the rock variable you defined (which is 'r'), not treat it as the string 'rock'. This causes your battle conditions to fail, and can even throw errors for inputs that aren't variable names. You need to use raw_input() to get plain string input from the user.
3. Mismatched computer Variable Type
You initialized computer as 'cactive' (a string), then later set it to a string like 'rock' via ran[randint(0,2)]. But all your battle checks look for computer==0, computer==1, or computer==2 (integers). These conditions will never be true, so none of the win/lose/tie logic runs — the loop just repeats the input prompt.
Fixed Code
import time from random import randint # Variable initialization status = 'yes' score = 0 # Map user input strings to clear integer values choices = {'rock': 0, 'paper': 1, 'scissors': 2} # Intro sequence print "===================================" time.sleep(1) print "Welcome To Rock, Paper, Scissors!" time.sleep(1) print "Please use lowercase for answers." time.sleep(1) print "===================================" time.sleep(1) raw_input("Are you ready to start? ") # Main game loop (all logic lives here) while status == 'yes': # Get user input (use raw_input() for Python 2 string handling) ques = raw_input('Choose Rock, Paper, Scissors, Score Or Exit: ').strip().lower() # Handle exit command first if ques == 'exit': print 'Thank You For Playing!' status = 'no' continue # Handle utility commands if ques == 'score': print "You Have", score, "Points!" continue if ques == 'cheat': score += 9999999999 print "Cheat Activated" time.sleep(1) continue if ques == 'clearscore': score = 0 print "Score Cleared!" continue if ques == 'cmd': print '------------------------------------------' print 'cmd - This List' print 'exit - Shutdown Game' print 'cheat - Gives Points' print 'clearscore - Clears The Current Score' print 'killcmd - Kills The Config (Not Advised)' print 'computer - shows what integer the computer chose (debug)' print '------------------------------------------' continue if ques == 'killcmd': print '*ERROR* CMD SHUTDOWN' status = 'no' continue # Validate game choice input if ques not in choices: print "Please enter a valid choice: rock, paper, scissors, score, exit, or cmd" continue # Generate computer's choice (integer 0=rock,1=paper,2=scissors) computer_choice = randint(0, 2) if ques == 'computer': print "Computer chose index:", computer_choice continue # Resolve battle outcome player_choice = choices[ques] if player_choice == computer_choice: print 'Tie!' # Win conditions: rock beats scissors, paper beats rock, scissors beats paper elif (player_choice == 0 and computer_choice == 2) or \ (player_choice == 1 and computer_choice == 0) or \ (player_choice == 2 and computer_choice == 1): print 'You Won!' score += 1 else: # Lose condition: get the computer's choice name for feedback comp_name = [k for k, v in choices.items() if v == computer_choice][0] print 'You Lost To', comp_name score -= 1
Key Fixes Explained
- Single main loop: All game steps run in one cycle, so each input triggers a full round of logic (or command handling) before asking for input again.
raw_input()instead ofinput(): Correctly captures user input as plain strings, avoiding unexpected variable lookups.- Consistent variable types:
computer_choiceuses integers that match your original battle condition logic, so win/lose/tie checks work as intended. - Input validation: Catches invalid inputs and tells the user exactly what options are allowed.
- Cleaner win/lose logic: Replaced repetitive
if/elifblocks with a concise, readable condition for win scenarios.
内容的提问来源于stack exchange,提问作者BlackKnight

