I'm continuing to experiment Python. *I've gotten to the point that the syntax almost doesn't bother me anymore. *But, I must say that I would like the python indent file of Vim to be a little bit more featureful.
I submitted a Rock-Paper-Cisor example to codeexamples.org, here it is, tell me what you think.
Code:
#!/usr/bin/env python
# rpc.py - copyright (c) 2001, Vincent Foley-Bourgon <gnuvince@yahoo.ca>
#
# This program is lisenced under the terms of the GNU General Public Lisence
# <http://www.gnu.org/copyleft/gpl.html>
# We will be needing random function
import random
# We first create an array containing the possible weapons (these are
# indexed from 0 to 2.
Weapons = ["Rock", "Paper", "Cisor"]
# Next we create a dictionnary in which we put the winning relations.
Winners = {"Rock" : "Cisor" , "Paper" : "Rock" , "Cisor" : "Paper"}
# I use global variables for the scores. *Some may say it's bad programming
# practice, but it's still useful to know how to do them.
score_player = 0
score_cpu = 0
# This function will display an evil error message if the user did some
# vile thing and will then wait for the user to press Enter.
def error_msg():
*print "\nInvalid option"
*print "Please press enter to continue"
*raw_input()
# This function determines who wins the current round. *It recieves two
# parameters: the weapon of player and the weapon of the computer
def determine_winner(player, cpu):
*# Define that these 2 variables are global
*global score_cpu
*global score_player
*
*# If both weapons are the same, return "Tie!"
*if player == cpu:
* *return "Tie!"
*# If the weapon that player's weapon beat is the same than the computer's
*# weapon, we add one to the score of player and we return a message saying
*# that player is the winner
*elif Winners[player] == cpu:
* *score_player += 1
* *return "You win!!!"
*# Same as above, except that now we check if the computer beats the player.
*elif Winners[cpu] == player:
* *score_cpu += 1
* *return "Computer wins... :("
# Main function
def main():
*# We must initialize option, else the program will crash on the next line
*option = ""
*# We play until the user inputs option 0
*while option != 0:
* *# Print and nice and dumb menu and ask for input
* *print "\n\t\t\t Rock-Paper-Cisor in Python \n"
* *print "1. Rock"
* *print "2. Paper"
* *print "3. Cisor"
* *print "\n0. Quit"
* *# We ask for input here. *If the user does something bad like Ctrl-C
* *# or Ctrl-D, we display an error message.
* *try:
* * *option = raw_input('Option: ')
* *except:
* * *error_msg()
* * *continue
* *# If we can't convert the user's input to an integer, it's probably because
* *# He input something else than a number. *In that case, we display
* *# our evil error message and we then redisplay the menu
* *try:
* * *option = int(option)
* *except:
* * *error_msg()
* * *continue
* * *
* *# We give a weapon to the player accordingly to the option he chose.
* *# if he chose an invalid option, we display and evil error message.
* *# and we redisplay the menu
* *if option == 1:
* * *weapon_player = Weapons[0]
* *elif option == 2:
* * *weapon_player = Weapons[1]
* *elif option == 3:
* * *weapon_player = Weapons[2]
* *elif option == 0:
* * *break
* *else:
* * *error_msg()
* * *continue
* *# Give the computer a random weapon
* *weapon_cpu = Weapons[random.randrange(3)]
* *# Let's see who wins!
* *result = determine_winner(weapon_player, weapon_cpu)
* *# Print what each player took and display the result of the round
* *print "\nPlayer chose *:", weapon_player
* *print "Computer chose:",weapon_cpu
* *print result
*# When the user quits, display the final score
*print
*print "Final Score"
*print "Player * * :",score_player
*print "Computer * :",score_cpu
* * *
if __name__ == '__main__':
*main()
Bookmarks