I think that there are some problems to be addressed and not all of them are about code.
If question is asked: "Does your powers work with heat or cold or both? " how one will know that 'F', 'C' or 'B' is expected? Why not accept 'heat', 'cold', 'both' as an answers?
This chain of if-s is very hard to maintain or change. Simpler approach would be creating mapping for powers (note my doubts about necessity of it expressed earlier):
powers_mapping = {'H': 'Fire',
'C': 'Ice',
'B': 'Both',
}
Now one can write simple validation function to get power from user. In order to have control over program flow instead of printing it returns validated value:
def validate(question, **kwargs):
while (answer := input(question).upper()) not in kwargs:
print(f'Expected one of {", ".join(kwargs)} but you entered {answer!r}')
return kwargs[answer]
Now it can be used like:
print(validate('Enter your power ...', **powers_mapping))
The advantage of this approach is that if need to update/change/add powers then you do it in power_mapping
and everything works just fine.
I put ...
in question to user. It should be replaced with values from mapping and not entered manually (one possibility is combination of f-string and power_mappings.values()
).
validate
function is general enough that it can be used to verify other mappings as well (weapons?). If this is not needed one can omit **kwargs
and access powers_mapping
directly.
It should be also noted that 3.8 <= Python is required due to walrus operator.