I am building an RPG style text based game in Python and I am stuck on a part. I am trying to ask for user input and use that in the code to select the players character and race. I have all the races as a class and the classes as a class.
Here is an example:
class Human:
hp = 50
dg = 10
ar = .1
cs = .05
ak = 5
ev = 0.1
class Elf:
hp = 20
dg = 20
ar = .1
cs = .2
ak = 15
ev = 0.15
def race_sel():
y = eval(input("Select Race: Human, Elf").capitalize())
return y
x = race_sel()
class Mage(x):
hp = 80 + x.hp
dg = 20 + x.dg
ar = .15 + x.ar
cs = .1 + x.cs
ak = 40 + x.ak
ev = .15 + x.ev
class Warrior(x):
hp = 100 + x.hp
dg = 20 + x.dg
ar = .2 + x.ar
cs = .1 + x.cs
ak = 30 + x.ak
ev = .15 + x.ev
def char_sel():
y = eval(input("Select Class: Mage, Warrior").capitalize())
return y
q = char_sel()
This semi works. In the console it asks the "String" then if I type, human or elf it .capitalize()
s the input and sets x
to the class selected. Then x
is plugged into one of the classes, either Mage(x)
or Warrior(x)
. Then it adds x.whatever
to the class that's selected.
Then the char_sel():
function selects what class the character is playing.
The issue I have is I wanted to use an if else statement in the functions: race_sel()
and class_sel()
where if the user input is not "human" or "elf" then it prints "Invalid selection, try again", but it seems with eval()
it automatically checks your input with all the Python classes you have in your code. Because eval()
changes it away from a str type.
Could I take a string input in then convert it to the eval()
type? I really am not very familiar with the eval()
method. It was just something I found so I could call my classes with user console input.
I am sure there is an easier way to do most of what I am writing.