- Today I learned about the role of return in the def function. return is used to return a value after the function runs, allowing it to be called in other code.
- The value returned by return is in memory, and needs to be assigned to a variable explicitly, and then printed to be seen.
- The scope of variable declaration is different, variables declared within a function only have an effect within the function. They have no effect outside the function.
- Today's challenge is a character generation game, first roll a die to determine how many characters to generate, then roll a 6*8 die to determine the characters' health.
CODE#
import random
print("Character Generation Game!")
def warrior(name):
n2 = random.randint(1, 6)
n3 = random.randint(1, 8)
health = n2 * n3
return health
n1 = random.randint(1, 6)
print("This time you will generate", n1, "characters")
for i in range(n1):
uname = input("Enter your character's name: > ")
print("Health:", warrior(uname))