Danny Yoo wrote:
is there any way in Python to simply add or subtract one from a
variable like i++ or i--? I don't mind doing a i = i + 1, but would
prefer something more simple and faster.
Out of curiosity, why are you incrementing variables?
Actually, it is mostly just my curiosity :) I'm designing my first
major Python program (It will run a EnGarde PBEM for me). I had to make
a loop to loop around 5 times and was going to use the i += 1 when I
remembered range :) I did the following instead and it worked perfectly.
for l in range(1,6):
print l
I will, eventually, be needing the i+= 1 as in the game many times I
will needing to take away or add to attributes. I have attached what I
got so far if you feel like taking a look, but it is probably nasty by
your standards :)
Thanks everyone for your answers. I'll probably have a few more
questions before I finish this project.
--
Your friend,
Scott
Sent to you from a Linux computer using Kubuntu Version 7.04 (Feisty Fawn)
#!/usr/bin/env python
'''This is a program to manage a PBEM EnGarde game using we=custom rules'''
def mfile_read():
'''This will open the master file and load all the variables'''
i = 0
try:
mfile = open("./Master/mfile", "r")
except:
mfile = open("./Master/mfile", "w")
print "File could not be opened, so I created one"
i = 1
if i == 1:
year = 1631
month = 8
else:
year = cPickle.load(mfile)
month = cPickle.load(mfile)
mfile.close()
return [year, month]
def mfile_save(year, month):
'''This will save the main variables into the master file'''
mfile = open("./Master/mfile", "w")
cPickle.dump(year, mfile)
cPickle.dump(month, mfile)
mfile.close()
return
def char_sheet(player, title, character, password, year, month, influence, cur_influence, sp, sl, income, funds, mid, mistress_name, mistress_sl, club, house, horses, regiment, rank, ma, appointment, strength, con, max_endurance, cur_endurance, rapier, dagger, sabre, cutlass, two_hand):
"""Create the text based character sheet"""
path = "./character_sheets/" + character
path.rstrip()
cfile = open(path, "w")
cfile.write("Engarde! Death with Honour\n--------------------------\n")
cfile.write(player)
cfile.write("\n")
if title:
s = title + " " + character
else:
s = character
cfile.write(s)
cfile.write("\nPassword: ")
cfile.write(password)
if month == 1:
month = "January"
elif month == 2:
month = "February"
elif month == 3:
month = "March"
elif month == 4:
month = "April"
elif month == 5:
month = "May"
elif month == 6:
month = "June"
elif month == 7:
month = "July"
elif month == 8:
month = "August"
elif month == 9:
month = "September"
elif month == 10:
month = "October"
elif month == 11:
month = "November"
elif month == 12:
month = "December"
s = "\n" + month + " " + str(year)
cfile.write(s)
s = "\n\nTotal Influence: " + str(influence)
cfile.write(s)
s = "\nCurrent Influence: " + str(cur_influence)
cfile.write(s)
s = "\nSocial Points: " + str(sp)
cfile.write(s)
s = "\nSocial Level: " + str(sl)
cfile.write(s)
s = "\n\nAllowance/Income: " + str(income)
cfile.write(s)
s = "\nMonth End Funds: " + str(funds)
cfile.write(s)
s = "\n\nMistress Name: " + mistress_name
cfile.write(s)
s = "\nMistress SL: " + str(mistress_sl)
cfile.write(s)
s = "\n\nClub: " + club
cfile.write(s)
s = "\nHouse: " + house
cfile.write(s)
s = "\nHorses: " + str(horses)
cfile.write(s)
s = "\n\nRegiment: " + regiment
cfile.write(s)
s = "\nRank: " + rank
cfile.write(s)
s = "\nMilitary Ability: " + str(ma)
cfile.write(s)
s = "\nMID: " + str(mid)
cfile.write(s)
s = "\nAppointment: " + appointment
cfile.write(s)
s = "\n\nStrength: " + str(strength)
cfile.write(s)
s = "\nConstitution: " + str(con)
cfile.write(s)
s = "\nMax Endurance: " + str(max_endurance)
cfile.write(s)
s = "\nCurrent Endurance: " + str(cur_endurance)
cfile.write(s)
s = "\nRapier: " + str(rapier)
cfile.write(s)
s = "\nDagger: " + str(dagger)
cfile.write(s)
s = "\nSabre: " + str(sabre)
cfile.write(s)
s = "\nCutlass: " + str(cutlass)
cfile.write(s)
s = "\nTwo-Handed Sword: " + str(two_hand)
cfile.write(s)
def char_save(player, title, character, password, family, influence, cur_influence, sl, allowance, funds, mid, mistress_name, mistress_sl, club, house, horses, regiment, rank, ma, appointment, strength, con, cur_endurance, rapier, dagger, sabre, cutlass, two_hand):
'''This saves the character to the harddrive'''
path = "./characters/" + player
cfile = open(path, "w")
cPickle.dump(player, cfile)
cPickle.dump(title, cfile)
cPickle.dump(character, cfile)
cPickle.dump(password, cfile)
cPickle.dump(family, cfile)
cPickle.dump(influence, cfile)
cPickle.dump(cur_influence, cfile)
cPickle.dump(sl, cfile)
cPickle.dump(allowance, cfile)
cPickle.dump(funds, cfile)
cPickle.dump(mid, cfile)
cPickle.dump(mistress_name, cfile)
cPickle.dump(mistress_sl, cfile)
cPickle.dump(club, cfile)
cPickle.dump(house, cfile)
cPickle.dump(horses, cfile)
cPickle.dump(regiment, cfile)
cPickle.dump(rank, cfile)
cPickle.dump(ma, cfile)
cPickle.dump(appointment, cfile)
cPickle.dump(strength, cfile)
cPickle.dump(con, cfile)
cPickle.dump(cur_endurance, cfile)
cPickle.dump(rapier, cfile)
cPickle.dump(dagger, cfile)
cPickle.dump(sabre, cfile)
cPickle.dump(cutlass, cfile)
cPickle.dump(two_hand, cfile)
def add_player_list(player):
'''This will add the players name to the list of players'''
plist = open("./Master/player_list", "a")
plist.write(player)
plist.write("\n")
plist.close()
return
def create_char():
"""This function creates a new character from a supplied text file"""
# ------------------------------- Read character creation sheet
create_form = open('create', 'r')
create_form.seek(7, 0)
player = create_form.readline()
player = player.rstrip()
create_form.seek(12, 1)
character = create_form.readline()
character = character.rstrip()
create_form.seek(16, 1)
alt_character = create_form.readline()
alt_character = alt_character.rstrip()
create_form.seek(11, 1)
password = create_form.readline()
password = password.strip()
password = password.lower()
create_form.seek(12, 1)
strength1 = create_form.readline()
strength1 = int(strength1)
create_form.seek(12, 1)
strength2 = create_form.readline()
strength2 = int(strength2)
create_form.seek(11, 1)
weakness = create_form.readline()
weakness = int(weakness)
s = 0
while s < 1 or s > 3:
print 'Is', player, 'the player you wanted?'
s = raw_input()
if s == 'y' or s == 'yes':
break
elif s == 'n' or s == 'no':
return
else:
print '\nplease select y, n, yes, or no\n'
while s < 1 or s > 3:
print '\nIs', character, 'a sufficient character name?'
s = raw_input()
if s == "y" or s == "yes":
break
elif s == "n" or s == "no":
while s < 1 or s > 3:
print '\nIs the alternate name', alt_character,
print 'a sufficient character name?'
s = raw_input()
if s == "y" or s == "yes":
character = alt_character
break
elif s == "n" or s == "no":
return
else:
print '\nplease select y, n, yes, or no\n'
break
else:
print '\nplease select y, n, yes, or no\n'
# -------------------------- Do Calculations
mfile = mfile_read() # Load the master file
year = mfile[0]
month = mfile[1]
if strength1 == weakness: # Check for generalized character
strength1 = 0
weakness = 0
if strength2 == weakness:
strength2 = 0
weakness = 0
if strength1 == 2 and strength2 == 2 and weakness == 3:
print ("Can not take funds as a strength and SL as a weakness")
return
family = "Gentleman"
influence = 0 # Give default, normal, values
sl = 5
allowance = 100
funds = 500
ma = 3
strength = 10
con = 10
rapier = 10
dagger = 10
sabre = 10
cutlass = 10
two_hand = 10
if strength1 == 2: # Strength 1
funds = 550
allowance = 110
elif strength1 == 3:
sl = 8
influence = 1
family = "Noble"
elif strength1 == 4:
strength = 13
con = 13
rapier = 13
dagger = 13
sabre = 13
cutlass = 13
two_hand = 13
elif strength1 == 5:
ma = 5
if strength2 == 2: # Strength 2
if strength1 == 2:
funds = 750
allowance = 125
else:
funds = 550
allowance = 110
elif strength2 == 3:
family = "Noble"
if strength1 == 3:
sl = 10
influence = 2
else:
sl = 8
influence = 1
elif strength2 == 4:
if strength1 == 4:
strength = 15
con = 15
rapier = 15
dagger = 15
sabre = 15
cutlass = 15
two_hand = 15
else:
strength = 13
con = 13
rapier = 13
dagger = 13
sabre = 13
cutlass = 13
two_hand = 13
elif strength2 == 5:
if strength1 == 5:
ma = 6
else:
ma = 5
if weakness == 2: #Weakness
funds = 250
allowance = 50
elif weakness == 3:
family = "Commoner"
sl = 1
elif weakness == 4:
strength = 8
con = 9
rapier = 4
dagger = 4
sabre = 4
cutlass = 4
two_hand = 4
elif weakness == 5:
ma = 1
# --------------------------- Save files
char_sheet(player, None, character, password, year, month, influence, influence, 0, sl, allowance, funds, 0, "None", 0, "None", "None", 0, "None", "None", ma, "None", strength, con, strength * con, strength * con, rapier, dagger, sabre, cutlass, two_hand)
mfile_save(year, month) # Save the master file
char_save(player, None, character, password, family, influence, influence, sl, allowance, funds, 0, "None", 0, "None", "None", 0, "None", "None", ma, "None", strength, con, strength * con, rapier, dagger, sabre, cutlass, two_hand)
add_player_list(player)
def delete_char():
"""This function will delete a selected character"""
pass
import cPickle
ans = 0
while ans != 3:
print 'Engarde Game Master v0.1.0'
print
print '1: Add New Character'
print '2: Delete Character'
print '3: Conduct a Turn'
print '4: Quit'
print
ans = raw_input('Please select from 1-4: ')
if ans == '1':
create_char()
elif ans == '2':
delete_char()
elif ans == '3':
break
elif ans == '4':
exit()
else:
print "\nYou did not select 1, 2, 3, or 4, you selected ", ans, "\n"
plist = open("./Master/player_list", "r")
for l in range(1,6):
print l
for x in plist:
print x
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor