- Today I learned about the function of
return
in thedef
function.return
is used to return a value after the function is executed, so that it can be called in other code. - The value returned by
return
is stored in memory, and it needs to be assigned to a variable explicitly in order to be displayed usingprint
. - The scope of a variable declaration is different. Variables declared within a function are only effective within the function. They have no effect outside the function.
- Today's challenge is a character generation game. First, generate a dice to determine how many characters to generate, and then generate a 6*8 dice 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 name: > ")
print("Health:", warrior(uname))