二手产品经理

二手产品经理

THIS IS RENO

Learn Python Online in 100 Days - Week 4 Day 4

Catbox

  1. 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.
  2. The value returned by return is in memory, and needs to be assigned to a variable explicitly, and then printed to be seen.
  3. 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.
  4. 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))
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.