diff --git a/Python/019_datastructures.py b/Python/019_datastructures.py index f2fb10c..e4f5f81 100644 --- a/Python/019_datastructures.py +++ b/Python/019_datastructures.py @@ -1,5 +1,6 @@ # Data Structures # Data structures hold sets of data in various formats +# We will discuss: list, dictionary (dict), tuple, set # List # Lists have collections of data @@ -73,4 +74,27 @@ print(f"Color tuple: R={color[0]} G={color[1]} B={color[2]}") # strings: "Cheese" # integers: 44 # lists: ['a', 'b', 'c'] -# objects: MyClass(param1, param2) \ No newline at end of file +# objects: MyClass(param1, param2) + +# Set +# Sets have collections of data +# set elements must be unique in the set +# Usually of the same type because uniqueness makes less sense otherwise +# with elements separated by commas +# where elements are unordered (i.e. can't reference specific element, slice, or index) +# are contained within curly braces +# Note: Once a set has been created its values cannot be changed, +# and elements cannot be added/removed + +# Declare our set +dice = {"1", "2", "3", "4", "5", "6"} +# Testing if an element exists is done with "in" +if "3" in dice: + print("The dice set contains a 3!") +# Iterate over a set +for die in dice: + print(f"dice element: {die}") +# NOTE!! You will see that the printed elements are in an arbitrary order. +# Because sets are unordered you cannot rely on which element comes +# before or after another element and will likely not match the order +# created inside the set declaration. \ No newline at end of file diff --git a/Python/Examples/dice.py b/Python/Examples/dice.py new file mode 100644 index 0000000..41ead19 --- /dev/null +++ b/Python/Examples/dice.py @@ -0,0 +1,13 @@ +dice = ["1", "2", "3", "4"] + +def parse_input(input_string): + if input_string.strip() in dice: + return int(input_string) + else: + print(f"Please enter a number from {dice[0]} to {dice[-1]}.") + raise SystemExit(1) + +num_dice_input = input(f"How many dice do you want to roll? [{dice[0]}-{dice[-1]}] ") +num_dice = parse_input(num_dice_input) +print(num_dice) +