Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79084
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'yet.': 0.04; 'elif': 0.05; 'subsequent': 0.05; 'diff': 0.07; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'starter': 0.09; 'url:activestate': 0.09; 'windows,': 0.09; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'wrote': 0.14; 'creates': 0.14; 'random': 0.14; "'n'": 0.16; '(none,': 0.16; 'beaten': 0.16; 'calculates': 0.16; 'determines': 0.16; 'func': 0.16; 'luck,': 0.16; 'menu.': 0.16; 'portable,': 0.16; 'quit.': 0.16; 'received:80.91.229.3': 0.16; 'received:97': 0.16; 'received:plane.gmane.org': 0.16; 'selects': 0.16; 'simplified': 0.16; 'subject:Rock': 0.16; 'true:': 0.16; 'win,': 0.16; 'looked': 0.18; 'entered': 0.20; 'code,': 0.22; 'input': 0.22; 'programming': 0.22; 'import': 0.22; 'previously': 0.22; 'print': 0.22; 'documented': 0.24; 'replace': 0.24; 'section.': 0.24; 'subject:Code': 0.24; "haven't": 0.24; 'options': 0.25; 'player': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'skills.': 0.29; 'url:code': 0.29; 'compared': 0.30; 'sets': 0.30; 'code': 0.31; 'easier': 0.31; 'occurs': 0.31; 'operations.': 0.31; 'probably': 0.32; 'another': 0.32; 'quite': 0.32; 'url:python': 0.33; 'cases': 0.33; 'trouble': 0.34; 'at:': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'next': 0.36; 'method': 0.36; 'thanks': 0.36; "i'll": 0.36; 'url:org': 0.36; 'should': 0.36; 'checks': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'break': 0.61; 'lower': 0.61; 'new': 0.61; 'url:3': 0.61; 'matter': 0.61; 'simple': 0.61; "you're": 0.61; 'save': 0.62; 'choose': 0.64; 'life': 0.66; 'here': 0.66; 'between': 0.67; 'determine': 0.67; 'beat': 0.68; 'invalid': 0.68; 'below:': 0.68; 'results': 0.69; 'paper': 0.75; '.....': 0.78; 'yourself': 0.78; 'beats': 0.84; 'draws': 0.84; 'loses': 0.84; 'played': 0.84; 'reading!': 0.84; 'ref': 0.84; 'replay': 0.84; 'score.': 0.84; 'selection,': 0.84; 'subject::': 0.85; 'habit': 0.91; 'scores': 0.91; 'online,': 0.96 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dave Angel <davea@davea.name> |
| Subject | Re:Code Review for Paper, Rock, Scissors |
| Date | Tue, 14 Oct 2014 06:23:47 -0400 (EDT) |
| Organization | news.gmane.org |
| References | <f267ac94-2bd4-4d46-a030-616b7d13e5e4@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | 97.73.240.9 |
| X-Newsreader | PiaoHong.Usenet.Client.VIP:1.62 |
| X-Archive | encrypt |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14841.1413282047.18130.python-list@python.org> (permalink) |
| Lines | 223 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1413282047 news.xs4all.nl 2895 [2001:888:2000:d::a6]:52484 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:79084 |
Show key headers only | View raw
Revenant <faceofoblivionofficial@gmail.com> Wrote in message:
>
>
....
> One thing I want to try to add is a "Press any key to continue" function that occurs at the end of the program when the user decides to quit. I looked at some options online, but haven't quite figured it out yet.
>
Use the curses function getch, as documented on:
https://docs.python.org/3/howto/curses.html
Or if you're on Windows, try msvcrt.getch.
If you're writing code that needs to be portable, look at:
http://code.activestate.com/recipes/577977-get-single-keypress/
> As previously stated, I am new to Python and would also like to see if any of you programming gurus have some suggestions about how I can simplify code, and also if there are any other good starter programs to work on to improve my skills.
>
> Thanks for reading! Code is below:
>
>
> # Creates the main menu.
> def menu():
> # Sets the scores to 0.
> global playerscore
> global compscore
> global draws
> playerscore = 0
> compscore = 0
> draws = 0
> menuselection = input('Please enter a selection: (Play/Help/About): ')
Save yourself trouble by using the lower method on the result of
each input function. I'll assume that for subsequent comments.
> # Checks for an invalid selection.
> while menuselection != 'Play' and menuselection != 'play' and menuselection != 'Help' and menuselection != 'help' \
> and menuselection != 'About' and menuselection != 'about':
Simplify to:
while menuselection not in ("play", "help", "about"):
> print('You have entered an invalid selection.')
> menuselection = input('\nPlease type a selection: (Play/Help/About): ')
> else:
> if menuselection == 'Play' or menuselection == 'play':
> play()
> elif menuselection == 'Help' or menuselection == 'help':
> instructions()
> else:
> about()
>
func = {"play":play, "help":instructions, "about":about}
func[menuselection]()
>
> # Creates the game.
> def play():
> global playerscore
> global compscore
> global draws
> # Player chooses Paper, Rock, or Scissors.
> playerselect = input('\nPlease choose Paper, Rock, or Scissors: ')
> # Checks for an invalid selection.
> while playerselect != 'Paper' and playerselect != 'paper' and playerselect != 'Rock' and playerselect != 'rock' \
> and playerselect != 'Scissors' and playerselect != 'scissors':
Again use playerselect not in ("pap... form
> print('You have entered an invalid selection.')
> playerselect = input('\nPlease choose Paper, Rock, or Scissors: ')
> else:
> if playerselect == 'Paper' or playerselect == 'paper':
> print('\nYou have selected Paper.')
> playerselect = 1
> elif playerselect == 'Rock' or playerselect == 'rock':
> print('\nYou have selected Rock.')
> playerselect = 2
> else:
> print('\nYou have selected Scissors.')
> playerselect = 3
> # Computer chooses Paper, Rock, or Scissors.
> import random
> compselect = random.randint(1, 3)
You could make life a little easier by using 0 to 2 instead, here
and above.
> if compselect == 1:
> print('The Computer has selected Paper')
> elif compselect == 2:
> print('The Computer has selected Rock.')
> else:
> print('The Computer has selected Scissors.')
print (None, "paper", "rock", "scissors")[compselect])
The next section can be simplified a lot by exploiting some
symmetries.
diff = (playerselect - compselect ) % 3
This number will be zero for a draw, 1 for computer win, and 2 for
player win. So you can have 3 cases below instead of
9.
> # Results if player selects paper.
> if playerselect == 1 and compselect == 1:
> print('Draw!')
> draws += 1
> score()
> else:
> if playerselect == 1 and compselect == 2:
> print('Paper beats rock. You win!')
> playerscore += 1
> score()
> elif playerselect == 1 and compselect == 3:
> print('Paper is beaten by scissors. You lose!')
> compscore += 1
> score()
> # Results if player selects rock.
> if playerselect == 2 and compselect == 2:
> print('Draw!')
> draws += 1
> score()
> else:
> if playerselect == 2 and compselect == 1:
> print('Rock is beaten by paper. You lose!')
> compscore += 1
> score()
> elif playerselect == 2 and compselect == 3:
> print('Rock beats scissors. You win!')
> playerscore += 1
> score()
> # Results if player selects rock.
> if playerselect == 3 and compselect == 3:
> print('Draw!')
> draws += 1
> score()
> else:
> if playerselect == 3 and compselect == 1:
> print('Scissors beat paper. You win!')
> playerscore += 1
> score()
> elif playerselect == 3 and compselect == 2:
> print('Scissors are beaten by rock. You lose!')
> compscore += 1
> score()
> again()
This makes me a little nervous. You have mutual recursion going on
between functions play and again. It probably won't matter here,
but it's a bad habit to get into.
What you really should do is something like
def play_once (): (current body of play function, but without
ref to again)
def play ():
while True:
play_once ()
if not again():
break
And change again so it returns True or False
>
> # Determines if the player wants to play another game.
> def again():
> replay = input('\nDo you want to play again (Y/N)? ')
> while replay != 'Y' and replay != 'y' and replay != 'N' and replay != 'n':
> print('You have entered an invalid selection.')
> replay = input('\nDo you want to play again (Y/N)? ')
> else:
> if replay == 'Y' or replay == 'y':
> play()
Replace the call to play () with
return True
> else:
> print('\nThanks for playing!')
>
return False
>
> # Creates the instructions.
> def instructions():
> print('\nPaper, Rock, Scissors is a simple game played against a computer opponent.')
> print('The player will have a choice of selecting paper, rock, or scissors.')
> print('The player\'s result will be compared with that of the computer to determine who wins the round.')
> print('In the event that both the player and the computer have the same selection, the round will end in a tie.')
> print('\nPaper beats rock but loses to scissors.')
> print('\nRock beats scissors but loses to paper.')
> print('\nScissors beats paper but loses to rock.')
> print('\nGood luck, and have fun!\n')
> menu()
>
>
> # Creates the about section.
> def about():
> print('\nPaper, Rock, Scissors\n\nVersion 1.0\n\nCreated by <Name>, 07 October 2014\n')
> menu()
>
>
> # Calculates score.
> def score():
> print('\nCurrent Scores: ')
> print('\nPlayer Score:', playerscore)
> print('\nComputer Score:', compscore)
> print('\nDraws:', draws)
>
>
> # Start of program operations.
> print('Welcome to Paper, Rock, Scissors!\n')
> menu()
>
--
DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Code Review for Paper, Rock, Scissors Revenant <faceofoblivionofficial@gmail.com> - 2014-10-14 01:04 -0700 Re: Code Review for Paper, Rock, Scissors Glenn Hutchings <zondo42@gmail.com> - 2014-10-14 01:39 -0700 Re:Code Review for Paper, Rock, Scissors Dave Angel <davea@davea.name> - 2014-10-14 06:23 -0400 Re: Code Review for Paper, Rock, Scissors Chris Angelico <rosuav@gmail.com> - 2014-10-15 00:35 +1100 Re: Code Review for Paper, Rock, Scissors Larry Hudson <orgnut@yahoo.com> - 2014-10-15 00:56 -0700
csiph-web