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 1
Oldest  Newest  Rating
+3 Isaiah Rahmany · August 31, 2014
Wow you should use this to introduce yourself to people I mean wow! You can go that far and you just started amazing!
You look like you have a very bright mind ;) 
+2 Mason Farina · August 31, 2014
Thank you sir! means a lot, I could say the same about your first program :)
+2 Kaveh Greenwood · August 31, 2014
It's great to see the works of others. Thanks for sharing! Keep it up!
0 Mason Farina · August 31, 2014
Thank you!
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
+2 Jake Scaife · September 1, 2014
Using r+ as far as I understand is fine. The issue is that when you try to write to a file using python, it will overwrite the entire file. Which is why, as you mentioned, when you run the program a second time, it simply overwrites the text rather than appending to it.

There is a few ways you can get around this, I would probably just append to the file.

If you use:
file = open('savedStats.txt','a')


This will open the file in append mode, so it will basically put anything you add to the file at the end of what is already in it.


You could also go the route of reading what is in the file and saving it as a string variable. Then simply adding whatever you need to, to the string.


hope this helps
  • 1
  • 2

Python

107,052 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator