FUNCTIONS don't just have to print things like we've been doing so far.
Instead, it can process data and return a value or set of values
This is called a RETURN VALUE
return statement takes a value from inside a function and sends it back to the line that called the function
This allows you to move a TON of the grunt work into functions!
What Can Be Returned From a Function??
Number, String, Boolean, Lists, Tuples, Dictionaries, other Functions and Generators (ADVANCED)
import random
def roll_die():
#Get a Random Number Between 1 and 6
roll = random.randint(1,6)
#Send the value back to whomever called it:
return roll
#Have a player roll a die and print the result
player_roll = roll_die()
print(f"You Rolled a {player_roll}")
Creates a function called roll_die
Generates a random number between 1 and 6
The returned value is assigned to the variable player_roll when the function is called.
The output displays the neatly formatted sentence showing the die roll.
TASK 1: Modify this die roller so it can take in a 4, 6, 8, 10, or 20 sided die
Min roll is always 1. Max roll is the number of sides
TASK 2: Create a function which rolls a pair of dice and returns the total
TASK 3: Create a function to roll a specific number of dice and return the total
YOU'LL NEED THIS CODE!!!!
The kingdom of Pythonia is at war! 500 warriors charge into battle, each swinging their mighty swords (rolling a D20 attack die). Your job? Analyze the battle statistics and determine:
1️⃣ How many critical hits (20s) ⚔️ and critical failures (1s) ❌ were rolled.
2️⃣ The most common roll (what number was hit the most).
3️⃣ The average attack roll (how strong was the army?).
4️⃣ The percentage of rolls above 10 (successful attacks).
5️⃣ The luckiest and unluckiest warrior (who rolled the highest and lowest individual rolls).
import statistics - There are some functions in here you should know about...