Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: collechess on August 04, 2011, 09:07:36 pm
-
So I just started learning python,(like 3 hours ago :)),and I created a text based rock, paper, scissors program. However, I cannot get it to allow the player to quit. Here is the code.
import random
while 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"
Thanks
-
I myself don't know Python, but is there a jump-like command, so you can jump to outside of the While True: loop?
-
Now, I don't know Python very well, I'm familiar with the syntax but haven't really done anything with it. However, I do think it would be possible, instead of saying 'While 1' or 'While True', a condition that loops infinitely, set up a boolean variable (or any kind) called Game_Is_Running, set it initially to true, or 1, or whatever, and you could say something like 'While Game_Is_Running == 1 or true or whatev. And then, where you say; if playerpick==quit: Game_Is_Running = 2 or someting like that, so the next iteration of the loop it would check if the variable is 1, and if it's not, terminate.
Some Code: (sorry I didn't put it in the BBcode "[ code ]" tag things I couldn't get bold to work with that )
import random
game_running = 1
While game_running==1:
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:
game_running = 2
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"
-
You could use a massive return( function to display if you win or lose, it would optimize this a lot, but would return True or False :(
I just went on CodingBat, which has a lot of nice Python practices (that's where I learned that ;))
EDIT: that wouldn't work, I don't think it displays it, only outputs data, but that might still be nice for other parts of the program
-
So, a few quick tips: adding three 'breaks' in a row won't make your program jump out of three loops -- the interpreter will see the first break, and will jump out of your while loop and ignore all the other 'breaks'.
Instead, either restructure your program so that it'll change a variable that'll let you quit, or import the module 'sys' and do 'sys.exit()'.
Also, lists in Python are awesomely useful. In this particular case, I was able to use lists to optimize away several 'if' statements.
Here's slightly more optimized version that I wrote:
import sys
import 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"
-
Try to avoid sys.exit, since it makes it difficult to embed your programs. Still a better version:
# A list containing all your choices.
choices = ["rock", "paper", "scissors", "quit"] #make "quit" another choice
while 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"
Edit: optionally, you can set playerpick to 'raw_input("choice: ").lower()', which allows use of both upper-, lower-, and mixed-case for input.
-
even better version:
import rock_paper_scissors
play()
:)
More seriously, good luck, but I personally can't help much.
-
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".
-
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".
*calcdude84se
There's nothing wrong with indexing AFAIK. In fact, you want an index because it makes determining win/lose easier.
-
thank you all for your help
-
even better version:
import rock_paper_scissors
play()
...is it sad that I actually tried this, and honestly believed it would work?
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.
-
even better version:
import rock_paper_scissors
play()
...is it sad that I actually tried this, and honestly believed it would work?
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.
Wait, you mean it didn't work? :( sad day for mankind