Merge branch 'master' of https://git.jaj.com/junior/BigSteve
This commit is contained in:
commit
db67d30080
|
@ -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)
|
||||
# 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.
|
13
Python/Examples/dice.py
Normal file
13
Python/Examples/dice.py
Normal file
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user