0 Members and 1 Guest are viewing this topic.
import randomwhile True: playerchoice=0 compchoice=random.randrange(1,3) outcome=0 quit="quit" rock="rock" paper="paper" scissors="scissors" while playerchoice==0: playerpick=raw_input("choice:") if playerpick==quit: break break break if playerpick==rock: playerchoice=1 elif playerpick==paper: playerchoice=2 elif playerpick==scissors: playerchoice=3 if compchoice==1: print "vs rock" if compchoice==2: print "vs paper" if compchoice==3: print "vs scissors" if playerchoice==compchoice: print "tie" if playerchoice==1 and compchoice==2: outcome=2 if compchoice==1 and playerchoice==2: outcome=1 if playerchoice==1 and compchoice==3: outcome=1 if compchoice==1 and playerchoice==3: outcome=2 if playerchoice==2 and compchoice==3: outcome=2 if compchoice==2 and playerchoice==3: outcome=1 if outcome==1: print "u win" elif outcome==2: print "u lose"
import sysimport random# A list containing all your choices.choices = ["rock", "paper", "scissors"]# I added an underscore because I think 'quit' is a keyword in python.quit_ = "quit"while True: playerchoice = 0 playerpick = "" # The computer will pick a number from zero up to (but not including) three. compchoice = random.randrange(0,3) # This way, it'll keep looping until you enter a valid input. while playerpick not in choices: playerpick = raw_input("choice:") if playerpick == quit_: # sys.exit() forces the program to quit. sys.exit() # Searches the list 'choices' and returns which position the string was in. playerchoice = choices.index(playerpick) # Optimization. print "vs " + choices[compchoice] if compchoice == playerchoice: print "tie" elif (compchoice + 1) % 3 == playerchoice: # Also an optimization. print "u win" else: print "u lose"
# A list containing all your choices.choices = ["rock", "paper", "scissors", "quit"] #make "quit" another choicewhile 1: #more idiomatic, and very slightly faster than "while True:" #No need to set either player* variable beforehand # The computer will pick a number from zero up to (but not including) three. compchoice = random.randrange(0,3) # This way, it'll keep looping until you enter a valid input. while playerpick not in choices: playerpick = raw_input("choice: ") #A stylistic choice, but I like spaces after my colons # Searches the list 'choices' and returns which position the string was in. playerchoice = choices.index(playerpick) #Quit if the player picked 3 ("quit") if playerchoice == 3: break #This successfully leaves the main loop # Optimization. print "vs " + choices[compchoice] if compchoice == playerchoice: print "tie" elif (compchoice + 1) % 3 == playerchoice: # Also an optimization. print "u win" else: print "u lose"
import rock_paper_scissorsplay()
Michael Lee, I suggest using random.choice() to choose randomly from a list. With calc84maniac's implementation this can't be done though because the bot could also choose "list".
even better version:Code: [Select]import rock_paper_scissorsplay()
Code: [Select]while 1: #more idiomatic, and very slightly faster than "while True:"
while 1: #more idiomatic, and very slightly faster than "while True:"
Quote from: Ashbad on August 05, 2011, 08:15:42 ameven better version:Code: [Select]import rock_paper_scissorsplay()...is it sad that I actually tried this, and honestly believed it would work?Quote from: calcdude84se on August 05, 2011, 08:13:28 amCode: [Select]while 1: #more idiomatic, and very slightly faster than "while True:"Huh, I didn't know that. It seems that's the case only for Python 2.x though -- in Python 3.x, there's no difference between the two, apparently.