fork download
  1. # your code goes here
  2. from collections import namedtuple
  3. import random
  4. import math
  5.  
  6. def ask(prompt, options):
  7. """General function for asking questions with options"""
  8. answer = None
  9. while answer not in options:
  10. answer = input(prompt + ' ({})'.format(', '.join(options)))
  11. return answer
  12.  
  13. Stats = namedtuple('Stats', 'hp attack defense')
  14.  
  15. CLASS_STATS = {
  16. 'archer': Stats(hp=35, attack=10, defense=4),
  17. 'rogue': Stats(hp=30, attack=12, defense=4),
  18. 'warrior': Stats(hp=50, attack=6, defense=8),
  19. 'mage': Stats(hp=30, attack=16, defense=2),
  20. 'cleric': Stats(hp=40, attack=8, defense=5)
  21. }
  22.  
  23. class Player():
  24. """Class to represent a player"""
  25.  
  26. @staticmethod
  27. def create():
  28. """Ask questions and create a player based on the answers."""
  29. good = 'no'
  30. while good != 'yes':
  31. user = input('What is your username? ')
  32. race = ask(prompt='What is your race?', options=['orc', 'human', 'elf', 'goblin'])
  33. p_class = ask(prompt='What is your class?', options=['archer', 'rogue', 'warrior', 'mage', 'cleric'])
  34. player = Player(user, race, p_class) # create the player
  35. good = ask(prompt='so you are {}? '.format(player), options=['yes', 'no'])
  36. return player
  37.  
  38. def __init__(self, username, race, p_class):
  39. """Initialize a new Player instance and set its attributes"""
  40. self.username = username
  41. self.race = race
  42. self.p_class = p_class
  43. self.stats = CLASS_STATS[p_class] # Assign starting stats
  44.  
  45. def __str__(self):
  46. """String representation of a Player object"""
  47. return "{} the {} {}".format(self.username, self.race, self.p_class)
  48.  
  49. def __repr__(self):
  50. """Python representation of a Player object"""
  51. return "Player('{}', '{}', '{}')".format(self.username, self.race, self.p_class)
  52.  
  53. player = Player.create()
  54.  
  55. def intro():
  56. print('''
  57. Welcome to the Realms of Sacar, {}!'''.format(player.username))
  58. print('''
  59. As a {}, your stats are: {}'''.format(player.p_class, player.stats))
  60. print('''
  61. You are an adventurer, you make your living off of collecting bounties on
  62. monsters and selling equipment and unique items you find on the way.
  63. ''')
  64. print('''At any time you can type "adventure" or "shop" to switch between
  65. battling and visiting the shop. You start with an inital 50g it's
  66. recommended that you visit the shop first.
  67. ''')
  68. intro()
  69.  
  70. monsters = ('fly', 'mouse', 'rat', 'serpent', 'goblin', 'gnome')
  71.  
  72. Monsters = namedtuple('type', 'hp attack defense')
  73.  
  74. MONSTERS_STATS = {
  75. 'fly': Fly(hp=10, attack=2, defense=1),
  76. 'mouse': Mouse(hp=20, attack=4, defense=1),
  77. 'rat': Rat(hp=25, attack=6, defense=3),
  78. 'serpent': Serpent(hp=40, attack=7, defense=3),
  79. 'goblin': Goblin(hp=55, attack=8, defense=4),
  80. 'gnome': Gnome(hp=55, attack=8, defense=8),
  81. }
  82.  
  83. while(True): #ignore this
  84. command = input()
  85. if command == 'adventure':
  86. print('adventure')
  87. if command == 'shop':
  88. print('shop')
  89.  
  90. print(MONSTERS_STATS[0])
Runtime error #stdin #stdout #stderr 0.04s 12696KB
stdin
Standard input is empty
stdout
What is your username? 
stderr
Traceback (most recent call last):
  File "./prog.py", line 53, in <module>
  File "./prog.py", line 31, in create
EOFError: EOF when reading a line