Start of my first python project

+6 Mason Farina · August 31, 2014

import random
stats = [
["Name:","placeholder","Level:",1,"Experience:",0,"Strength:",0],
]
enemyStats = [
["Level:",1,"Strength:",0],
]
stats[0][7] = random.randrange(10,20)
enemyStats[0][3] = random.randrange(10,20)
name = input("Please enter a name for your character: ")
stats[0][1] = name
if name == 'debug':
enemyStats[0][3] = 0
print(stats[0][0],stats[0][1],stats[0][2],stats[0][3],stats[0][4],stats[0][5],stats[0][6],stats[0][7])
_stats = input("These are your stats, type 'reroll' to re-roll your stats or 'done' to finish: ")
if _stats == "reroll":
stats[0][7] = random.randrange(10,20)
else:
print("Continuing..")
pAttack = stats[0][7] / 2 * stats[0][3]
eAttack = enemyStats[0][3] / 2 * enemyStats[0][1]
print(pAttack,eAttack)
def combat(enemyAttack,playerAttack):
if playerAttack > enemyAttack:
print(name,"wins")
elif enemyAttack > playerAttack:
print("Enemy wins")
else:
print("It's a tie")
combat(eAttack,pAttack)

So, Brand new to python, just started when Bucky started posting his videos, I went ahead and started making a game, because I like to challenge myself. I have no previous knowledge of Python, but some LUA knowledge which really sped up my process. Heres my code so far.


It's not even close to being done, but it can be fun to mess with for a few minutes..I still need to comment out a few things, and clean up some of those table arrays and wrap it into a shell of some sort and I still need to figure out how to use external files for saving statistics and such, all in due time! Let me know what you guys think c:

~Inumel

edit:sorry for the repost, I deleted the original c:

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Mason Farina · August 31, 2014
I  have been looking into the file system, but I can't seem to get a grasp on it. Here is some example code.

file = open('savedStats.txt','r+')

for f in range(0,6):
    file.write('f'+"\n")
file.close

This posts the letter "f" on a file six times, as expected. but it seems to.. reset, with every iteration of the program starting. I was wondering if there was any way to write to files, and... save it? so i can keep the stuff on the file AFTER the build has ended. I have been researching this, but if anybody has the answer I would be most appreciative.
0 Isaiah Rahmany · August 31, 2014
you have "r+", r is for reading and if you want to read only you put "r" if you want write only you put "w" if you want both you put "r+w", hope this fixes your problem!

for example in order to read and write


file = open("file.txt", "r+w")

file.write("Hello world\n")

file.close()
0 Mason Farina · August 31, 2014
I tried that at first, but got this error

ValueError: must have exactly one of create/read/write/append mode

Then i did some digging on the internet and found that "r+" could do both.. ill do more digging though, thanks!
0 Isaiah Rahmany · August 31, 2014
yea your right actually I did read the docs just now
0 Isaiah Rahmany · August 31, 2014
what I did was make 2 objects one for reading and one for writing... Idk if you wana do that
0 Mason Farina · September 1, 2014
That should help quite a bit! Thank you!
0 Mason Farina · September 1, 2014
Heres an update! I have started integrating my saved statistics file, and i'm starting to attempt to re-save to to said file c:


import random
import test
import math
import tkinter
_readstats = open('savedStats.txt','r+')
_savedStats = [_readstats.read().splitlines()]
stats = [
    ["Name:","placeholder","Level:",1,"Experience:",0,"Strength:",0],
]
enemyStats = [
    ["Level:",1,"Strength:",0],
]
_expchart = [

]
print(_savedStats[0][3])
#print(_teststat)
stats[0][7] = random.randrange(10,20)
enemyStats[0][3] = random.randrange(10,20)
if _savedStats[0][2] == "placeholder":
    name = input("Please enter a name for your character: ")
    stats[0][1] = name
    if name == 'debug':
        enemyStats[0][3] = 0
name = _savedStats[0][1]
print(stats[0][0],stats[0][1],stats[0][2],stats[0][3],stats[0][4],stats[0][5],stats[0][6],stats[0][7])
_stats = input("These are your stats, type 'reroll' to re-roll your stats or 'done' to finish: ")
if _stats == "reroll":
    stats[0][7] = random.randrange(10,20)
else:
    print("Continuing..")
pAttack = stats[0][7] / 2 * int(_savedStats[0][3])
eAttack = enemyStats[0][3] / 2 * enemyStats[0][1]
print(pAttack,eAttack)
wins = int(_savedStats[0][9])
losses = int(_savedStats[0][11])
ties = int(_savedStats[0][13])
def combat(enemyAttack,playerAttack):
    if playerAttack > enemyAttack:
        print(name,"wins")
        return wins
    elif enemyAttack > playerAttack:
        print("Enemy wins")
        return losses
    else:
        print("It's a tie")
        return ties
_combat = combat(eAttack,pAttack)
if pAttack > eAttack:
    wins  += 1
elif eAttack < pAttack:
    losses += 1
else:
    ties +=1

I'm sticking with the "r+" file mode, Ill unpack all of the contents of the file, assign them to variables in the main program, and then re-save them once the program has ended.
0 Mason Farina · September 1, 2014
I always appreciate constructive criticism! More white spaces where? In between the lines? Or in between the commas and such, seperating different objects?

like

this

Or
x=5*15, into x = 5 * 12

I'll be happy to change it :)
0 Mason Farina · September 2, 2014
Thanks! Going to be another long day of messing with this again, i think :P
  • 1
  • 2

Python

107,329 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator