Skip to content

Instantly share code, notes, and snippets.

@averyfreeman
Created February 19, 2024 06:34
Show Gist options
  • Save averyfreeman/988756060f776b4bdf1244ceff2461bc to your computer and use it in GitHub Desktop.
Save averyfreeman/988756060f776b4bdf1244ceff2461bc to your computer and use it in GitHub Desktop.
```
wiz = ["Wizard",70,150]
elf = ["Elf",70,100]
hum = ["Human",150,20]
dragon = ["Trump",300,150]
print("Welcome to Function Scope Issue Example!")
print(
f"1) {wiz[0]}\n"
f"2) {elf[0]}\n"
f"3) {hum[0]}\n"
)
def main_menu():
while True:
_v = input("Choose your character: ")
if _v == "1":
print(
f"You have chosen {wiz[0]}\n"
f"Hit points: {wiz[1]}\n"
f"Damage points: {wiz[2]}\n"
)
fight(wiz)
break
elif _v == "2":
print(
f"You have chosen {elf[0]}\n"
f"Hit points: {elf[1]}\n"
f"Damage points: {elf[2]}\n"
)
fight(elf)
break
elif _v == "3":
print(
f"You have chosen {hum[0]}\n"
f"Hit points: {hum[1]}\n"
f"Damage points: {hum[2]}\n"
)
fight(hum)
break
else:
print("Unknown character", _v)
continue
return
def fight(char):
count = 1
while (char[1] > 0) and (dragon[1] > 0):
count = (count + 1) % 2
if count == 1:
attacker = dragon
victim = char
victim[1] = (victim[1] - attacker[2])
print(
f"The {attacker[0].lower()} damaged the {victim[0].lower()}!\n"
f"The {victim[0].lower()}'s hitpoints are now: {victim[2]}\n"
)
elif count == 0:
attacker = char
victim = dragon
victim[1] = (victim[1] - attacker[2])
print(
f"The {attacker[0].lower()} damaged the {victim[0].lower()}!\n"
f"The {victim[0].lower()}'s hitpoints are now: {victim[2]}\n"
)
continue
else:
print(
f"The {attacker[0].lower()} has slain the {victim[0].lower()}!\n"
f"The {victim[0].lower()} has lost the battle... \U0001F62D\n"
)
return
main_menu()
```
Code snippet I recreated using lists instead of dicts for the characters. Was hoping to re-create a scoping issue I was having, but am unable - the code appears to work fine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment