startup = 0
r = rock
p = paper
s = scissors

if startup == 0:
    weapon = str(input("Please enter your weapon: Rock, Paper, or Scicors")
        while weapon =! r:
        print("Incorrect choice, please choose again")

Im running python 3.2.2. Im trying to make rock paper scissors. I am trying to get the if statement to startup and print the str weapon. and within that i have a while loop (where the error is on the while) and i want that to print and loop back to the toop if something other than rock, paper, or scissors is inputted. any suggestions?

Dani AI

Generated

Quick diagnosis and a simple plan. already spotted syntax problems (unclosed parentheses, undefined names). The original snippet also uses the wrong operator (=! instead of !=), and has comparisons like if "rock" and "scissors": which always evaluate True because non-empty strings are truthy. Because this thread is about Python 3 (3.2.x), use input() (not raw_input) and normalize the result with .strip().lower() before validating.

Loop structure that matches the feature list in Post #5:

  • Outer loop for rounds that runs until the player quits.
  • Inner validation loop that keeps asking until the player types a valid weapon (or a quit command).
  • Pick the computer’s choice only after the player locks in theirs so it stays hidden.
  • Decide the winner with a deterministic rule (use a mapping of what beats what) and update scores.
  • Print the round result and repeat; print final score on quit.

A concise Python 3 example implementing that structure:

import random

def get_weapon(prompt, valid):
    while True:
        choice = input(prompt).strip().lower()
        if choice in ('q', 'quit'):
            return 'quit'
        if choice in valid:
            return choice
        print("Invalid choice: '{}'".format(choice))

def decide_winner(user, comp):
    beats = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}
    if user == comp:
        return 'tie'
    return 'user' if beats[user] == comp else 'computer'

def play():
    weapons = ('rock', 'paper', 'scissors')
    user_score = comp_score = 0
    round_no = 1
    while True:
        print("\nRound {}".format(round_no))
        user = get_weapon("Choose [rock/paper/scissors] (q to quit): ", weapons)
        if user == 'quit':
            break
        comp = random.choice(weapons)
        print("Computer chose: {}".format(comp))
        result = decide_winner(user, comp)
        if result == 'tie':
            print("Tie.")
        elif result == 'user':
            print("{} beats {} — you win.".format(user.capitalize(), comp))
            user_score += 1
        else:
            print("{} beats {} — computer wins.".format(comp.capitalize(), user))
            comp_score += 1
        round_no += 1
    print("\nFinal score: You {} - Computer {}".format(user_score, comp_score))

if __name__ == '__main__':
    play()

Troubleshooting notes: test inputs with extra spaces and different cases; spell the weapon names correctly; watch indentation and matching parentheses; handle Ctrl+C if needed. This follows the input-validation idea from and the simplification suggested by , while keeping the code Python 3–friendly for ’s requested features.

Recommended Answers

All 4 Replies

The () are not closed properly at line 7, the variables rock, paper, scissors are not defined.

import random

#i think you wanted strings here
r = "rock"
p = "paper"
s = "scissors"

#a list of the weapons
weapons = [r, p, s]

#you need to indent everything you want looped

while True: 

    #raw input is usually used instead of input - see the docs
    user_choice = raw_input("Choose yer weapon [rock/paper/scissors]: ")

    #restarts the loop if the typed response not in the list of weapons
    #using .lower makes it case insensitive
    if user_choice.lower() not in weapons:
        print "%s is not a valid choice!" % user_choice
        continue

    #otherwise break out of the loop
    else:
        break

print "You wield the mystical %s of +1 Awesomeness." % user_choice
computer_choice = random.choice(weapons)
print "A wild programmer appears, swinging a deadly %s!" % computer_choice

You can simplify this as follows (continue and sometimes break often signifies a better logic exists).

import random
 
#i [I] think you wanted strings here
#r = "rock"
#p = "paper"
#s = "scissors"
 
#a list of the weapons
weapons = ["rock", "paper", "scissors"]     ## separate variables are not necessary
 
#you need to indent everything you want looped
user_choice="" 
while user_choice.lower() not in weapons:
 
    #raw input is usually used instead of input - see the docs
    #-----> unless OP is on Python3X
    user_choice = raw_input("Choose yer weapon [rock/paper/scissors]: ")
    if user_choice.lower() not in weapons:
        print "%s is not a valid choice!" % user_choice
 
print "You wield the mystical %s of +1 Awesomeness." % user_choice
computer_choice = random.choice(weapons)
print "A wild programmer appears, swinging a deadly %s!" % computer_choice

The program will choose a weapon (Rock, Paper, Scissors), but its choice will not be displayed until
later so the user doesn‟t see it.
The program will announce the beginning of the round and ask the user for his/her weapon choice
The two weapons will be compared to determine the winner (or a tie) and the results will be displayed
by the program
The next round will begin, and the game will continue until the user chooses to quit
The computer will keep score and print the score when the game end

How do i incorporate these with loops? Im having a hard time trying to figure it out. the code above was all correct but not towards what im shooting for. this is what i have so far

import random
 
#strings here
r = "rock"
p = "paper"
s = "scissors"
 
#a list of the weapons
weapons = [r, p, s]

computer_choice = random.choice(weapons)
user_choice = input("Choose yer weapon [rock/paper/scissors]: ")

if user_choice.lower() not in weapons:
    print ("This is not a valid choice!", user_choice)

while computer_choice != user_choice:
    print("Rock, paper, scissor, shoot!")
    if "rock"and "scissors":
        print("Rock Smashes scissors")
    elif "scissors" and "paper":
        print("Scissors cut paper")
    elif "paper" and "rock":
        print("Paper covers rock")
    break

else:
    print("Tie ,try again...")
    user_choice = input("Choose yer weapon [rock/paper/scissors]: ")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.