def create_char():
""" Player chooses a hero class"""
name = ""
while name == "":
name = raw_input("What would you like to name your character? ").upper()
char_class = None
while char_class not in ('WARRIOR', 'SORCEROR', 'THEIF'):
char_class = raw_input("\nIs your character a 'WARRIOR', 'SORCEROR', or 'THEIF'? ").upper()
print "\nOk, your character,", name, "is a", char_class
return name, char_class
# Functions for all 3 character classes, sets up initial stats, inv, and equipment
def warrior():
""" Defines the Warrior class"""
inventory = ["Healing Potion", "Mana Potion", "Torch"]
equipment = ["Worn Battle Axe", "Rusty Helm"]
attributes = {"Strength" : 10, "Vitality" : 10, "Dexterity" : 5, "Energy" : 0}
print "\nYour Hero is currently equipped with: ", equipment, "\n"
print "Your Heroes inventory consists of: ", inventory, "\n"
print "Your Heroes current attributes are: "
for stat in attributes.keys():
print stat, "----", attributes[stat]
def sorceror():
""" Defines the Sorceror class"""
inventory = ["Healing Potion", "Mana Potion", "Torch"]
equipment = ["Worn Short Staff", "Worn Robe"]
attributes = {"Strength" : 0, "Vitality" : 10, "Dexterity" : 5, "Energy" : 10}
print "\nYour Hero is currently equipped with: ", equipment, "\n"
print "Your Heroes inventory consists of: ", inventory, "\n"
print "Your Heroes current attributes are: "
for stat in attributes.keys():
print stat, "----", attributes[stat]
def theif():
""" Defines the Theif class"""
inventory = ["Healing Potion", "Mana Potion", "Torch"]
equipment = ["Worn Dagger", "Worn Bandits Mask"]
attributes = {"Strength" : 5, "Vitality" : 10, "Dexterity" : 10, "Energy" : 10}
print "\nYour Hero is currently equipped with: ", equipment, "\n"
print "Your Heroes inventory consists of: ", inventory, "\n"
print "Your Heroes current attributes are: "
for stat in attributes.keys():
print stat, "----", attributes[stat]
def main():
name, char_class = create_char()
if char_class == "WARRIOR":
warrior()
elif char_class == "SORCEROR":
sorceror()
elif char_class == "THEIF":
theif()
# Start
main()
raw_input("\nPress Enter to exit")
🔒 Locked
Help my practicing :)
Advertisement
Original Post
Since im trying to practice python in any way possible, I decided to create a sort of intro to a text based RPG. It simply creates a character with a name and class, then displays the stats and initial inventory for the new player. Id like to create a system that allows a player to allocate an additional 20 stat points into the attributes of their choice but im having trouble figuring out how to get this done. Can someone please critique this program in every way possible? lol like tell me if it was a bad way to represent what i was trying to acheive and how it could be done better, faster, more efficiently, etc.
Ok, try the following:
- rewrite the print statements to use string interpolation rather than commas
- fix the "for stat in attributes.keys()" loop so that you don't need to look up the actual value inside the loop (hint - don't use keys() )
- rewrite the print statements to use string interpolation rather than commas
- fix the "for stat in attributes.keys()" loop so that you don't need to look up the actual value inside the loop (hint - don't use keys() )
Quote:
Original post by Ezbez
The comments in his code:def warrior(): """ Defines the Warrior class""" ...
Unless I'm seriously misinterpreting this and he's referring to in-game character classes, which seems increasingly likely as I think about it.
hehe ya man you got it :) i was refering to class in the context of a hero class (warrior, mage, etc) Not a class as in OOP. Sorry I shouldnt have used that word. And to Kylotan : Theres a way to print the items of your dictionary without actually accessing the keys? I never learnt this. Also the string interpolation thing isnt making sense to me, what is it replacing in my code and why is it better??
Quote:
Original post by biggjoee5790
And to Kylotan : Theres a way to print the items of your dictionary without actually accessing the keys?
You will still access 'the keys'. You just won't call 'keys()' to get them. There are other functions on dictionaries other than keys(), so take a look through the tutorial to see what is available.
Quote:
Also the string interpolation thing isnt making sense to me, what is it replacing in my code and why is it better??
It replaces the stuff in print statements where you use commas to separate literal text from variables. If you wanted to format the variables first (eg. trim a floating point number so it only showed a certain number of decimal places), or you just needed it in a string rather than sent to the print command, string interpolation is the way to go.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement