(hopefully easy) Python functions and variables not working

Hi, I am a beginner coder and I want to use functions in my code, however there is an obvious problem.

code:
num1 = 50
num2 = 10
num3 = 5
num4 = 0
num5 = 0

def one(two):
    return(f"""Stats:
    health {num1}
    defense {num2}
    damage {num3}
    madness {num4}
    rage {num5}
    """)

variable1 = input("""What would you like to do?
You can Run, Show Stats, Attack: """)
variable1 = variable1.lower()
if variable1 == 'run':
    print('battle has ended')
elif variable1 == one:
    print('stats shown')
elif variable1 == 'attack':
    print('you attacked')
else:
    print("You've inputted something wrong! Try again.")

the error is the elif variable1 == one, if it doesn’t make sense whatsoever please reply.

I’m trying to use functions because for a game that i am creating here, my superior says that i should be using functions in order to make the code look cleaner.

All help is greatly appreciated.

Is this closer to what you want?

num1 = 50
num2 = 10
num3 = 5
num4 = 0
num5 = 0


def stats():
    return(f"""Stats:
    health {num1}
    defense {num2}
    damage {num3}
    madness {num4}
    rage {num5}
    """)


variable1 = input("""What would you like to do?
You can Run, Show Stats, Attack: """)
variable1 = variable1.lower()
if variable1 == 'run':
    print('battle has ended')
elif variable1 == 'show stats':
    print(stats())
    print('stats shown')
elif variable1 == 'attack':
    print('you attacked')
else:
    print("You've inputted something wrong! Try again.")
3 Likes

there are a couple of ways to avoid using if-else here,

nums = [50, 10, 5, 0, 0]
k = (i for i in nums)
stats = ''.join([f'{j} {next(k)}' for j in ['health', ' defense', ' damage', 
                                            ' madness', ' rage']])

from collections import defaultdict
x = defaultdict(lambda: "You've inputted something wrong! Try again.", 
                {"run": "battle has ended", 
                 'show_stats': f"Stats: {stats}", 
                 "attack": "you attacked"}) 

x[input("What would you like to do?")]
import numpy as np
x = input("what would you like to do?")
condlist = [(x == i) for i in ['run', 'show_stats', 'attack']]
choicelist = ["battle has ended", f"Stats: {stats}", "you attacked"]
np.select(condlist, choicelist, "You've inputted something wrong! Try again.").item()

also not sure if a function is required here, as a function would usually be made if there is repetitive code, but that does not appear to be the case here.

1 Like

It might be useful to keep data and it’s display separate. Keeping data in tuple/list/dictionary will give dynamic access to data.

If you have already defined values and stats then you can create dictionary easily:

>>> values = [50, 10, 5, 0, 0]
>>> keys = ["health", "defence", "damage", "madness", "rage"]
>>> stats = dict(zip(keys, values))
>>> stats
{'health': 50, 'defence': 10, 'damage': 5, 'madness': 0, 'rage': 0}

If you create them by hand then you could write right away:

>>> stats = {'health': 50, 'defence': 10, 'damage': 5, 'madness': 0, 'rage': 0}

Same applies to activities. It will be cumbersome if you would like to add new activity “defend” if you have hardcoded activities. Alternatively:

>>> activities = ("Run", "Show Stats", "Attack")
>>> print(f"What would you like to do? You can {', '.join(activities)}:")
What would you like to do? You can Run, Show Stats, Attack:

This way you just need to add new item to activities and it will be included in display. It might be dictionary as well if you need to attach something to each activity (printout message?).

To print stats you can do:

>>> print(*(f"{k}: {v}" for k, v in stats.items()), sep='\n')
health: 50
defence: 10
damage: 5
madness: 0
rage: 0

To change stats you can:

>>> stats['health'] += 50
>>> stats['health']
100

As side effect your code will be more readable. If you have num1…num5 and when you change them you probably should check and double check whether num3 was defence or damage.

3 Likes