Learning Python Part 1

Best way to learn is to do.  First idea was a Rock+Paper+Scissor Machine.

This probably is not the most effect way but it works…

import random

def get_var_value(filename="varstore0.dat"):
with open(filename, "a+") as f:
val = int(f.read() or 0) + 1
f.seek(0)
f.truncate()
f.write(str(val))
return val

foo = ['Rock', 'Paper', 'Scissor']
foo2 = ['Rock', 'Paper', 'Scissor']

select1 = (random.choice(foo))
select2 = (random.choice(foo2))

print "Player 1 %s" % select1
print "Player 2 %s" % select2

if select1 == select2:
print("Tie")
tie_counter = get_var_value("tie.dat")
print("{} Ties.\n".format(tie_counter))

if select1 == 'Rock' and select2 == 'Paper':
print ("Player 2 Wins - Paper Wins")
p2_counter = get_var_value("p2.dat")
print("Player 2 has won {} Times.\n".format(p2_counter))

if select1 == 'Paper' and select2 == 'Rock':
print ("Player 1 Wins - Paper Wins")
p1_counter = get_var_value("p1.dat")
print("Player 1 has won {} Times.\n".format(p1_counter))

if select1 == 'Rock' and select2 == 'Scissor':
print ("Player 1 Wins - Rock Wins")
p1_counter = get_var_value("p1.dat")
print("Player 1 has won {} Times.\n".format(p1_counter))

if select1 == 'Scissor' and select2 == 'Rock':
print ("Player 2 Wins - Rock Wins")
p2_counter = get_var_value("p2.dat")
print("Player 2 has won {} Times.\n".format(p2_counter))

if select1 == 'Paper' and select2 == 'Scissor':
print ("Player 2 Wins - Scissor Wins")
p2_counter = get_var_value("p2.dat")
print("Player 2 has won {} Times.\n".format(p2_counter))

if select1 == 'Scissor' and select2 == 'Paper':
print ("Player 1 Wins - Scissor Wins")
p1_counter = get_var_value("p1.dat")
print("Player 1 has won {} Times.\n".format(p1_counter))

Let the script fun for a few hours.

Tie 19006 Ties.
Player 1 has won 19052 Times
Player 2 has won 19161 Times.

Leave a Reply

Your email address will not be published. Required fields are marked *