Skip to content

Commit 875956f

Browse files
updated
1 parent b916218 commit 875956f

7 files changed

Lines changed: 149 additions & 1 deletion

File tree

30-dice-roller-program/main.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# dictionary is a combination of key:value pairs in a curly braces {}
1616

17-
# the value is a tuple of strings (matrix of characters) representing each line of the dice face
17+
# The value is a tuple of 5 strings in matrix form representing each line of the dice
1818

1919
dice_art = {
2020
1: ("┌─────────┐",
@@ -56,14 +56,23 @@
5656

5757
def roll_dice(num_dice):
5858
"""Roll the specified number of dice and display the results"""
59+
60+
# If user enters less than 1 dice, prompt for valid input
61+
5962
if num_dice < 1:
6063
print("Please enter a valid number of dice (1 or more)")
6164
return
6265

66+
# construct a list to hold the results of each dice roll
6367
results = []
68+
69+
# for each dice to be rolled, generate a random number between 1 and 6
70+
# image this as rolling 6 physical dice on a table
71+
6472
for _ in range(num_dice):
6573
results.append(random.randint(1, 6))
6674

75+
6776
# Display dice art
6877
print("\n" + "="*50)
6978
print(f"Rolling {num_dice} dice...")
@@ -76,6 +85,8 @@ def roll_dice(num_dice):
7685
line += dice_art[result][line_index] + " "
7786
print(line)
7887

88+
# Display results and total
89+
7990
print("\n" + "="*50)
8091
print(f"Results: {results}")
8192
print(f"Total: {sum(results)}")

30-dice-roller-program/try.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import random
2+
3+
dice_art = {
4+
1: ("┌─────────┐",
5+
"│ │",
6+
"│ ● │",
7+
"│ │",
8+
"└─────────┘"),
9+
10+
2: ("┌─────────┐",
11+
"│ ● │",
12+
"│ │",
13+
"│ ● │",
14+
"└─────────┘"),
15+
16+
3: ("┌─────────┐",
17+
"│ ● │",
18+
"│ ● │",
19+
"│ ● │",
20+
"└─────────┘"),
21+
22+
4: ("┌─────────┐",
23+
"│ ● ● │",
24+
"│ │",
25+
"│ ● ● │",
26+
"└─────────┘"),
27+
28+
5: ("┌─────────┐",
29+
"│ ● ● │",
30+
"│ ● │",
31+
"│ ● ● │",
32+
"└─────────┘"),
33+
34+
6: ("┌─────────┐",
35+
"│ ● ● │",
36+
"│ ● ● │",
37+
"│ ● ● │",
38+
"└─────────┘")
39+
}
40+
41+
42+
def roll_dice(number_of_dice_to_roll):
43+
""" Enter Number of dices to throw between 1 and 6 and calculate score"""
44+
print(f"you asked me to roll,{number_of_dice_to_roll}")
45+
46+
results = []
47+
48+
for i in range (int(number_of_dice_to_roll)):
49+
results.append(random.randint(1,6))
50+
print(f'results: {results}')
51+
sum_of_results = sum(results)
52+
print(f"sum: {sum_of_results}")
53+
54+
# print the dices
55+
56+
57+
for result in results:
58+
for line_index in range(5):
59+
print(dice_art[result][line_index])
60+
61+
def main():
62+
while True:
63+
try:
64+
num_of_dice = input("Enter number of dice you would like to throw !! ( 'q' or 'quit' to exit game): " ).strip()
65+
66+
if num_of_dice.lower() == 'q' or num_of_dice == 'quit':
67+
print("thanks for playing")
68+
break
69+
elif int(num_of_dice) < 6 and int(num_of_dice) > 1:
70+
roll_dice(num_of_dice)
71+
else:
72+
"provide a valid input..enter 1 to 6 to roll dice or q to quit"
73+
except ValueError:
74+
print(f"value error")
75+
76+
if __name__ == "__main__":
77+
main()

31-functions/create_name.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# return = statement used to end a function
2+
#. and send a result back to the caller
3+
4+
5+
def create_name(first,last):
6+
first = first.capitalize()
7+
last = last.capitalize()
8+
return first + " " + last
9+
10+
full_name = create_name("spongebob", "squarepants")
11+
12+
print(full_name)

31-functions/invoice.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def display_invoice(username, amount, due_date):
2+
print(f"Hello, {username}")
3+
print(f"Your bill amount of ${amount:.2f} is due: {due_date}")
4+
5+
6+
display_invoice("JohnDoe", 100.09, "01/02")

31-functions/main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def happy_birthday(name, age):
2+
print(f"happy birthday, {name}!")
3+
print(f"you are {age} years old!")
4+
print("happy birthday !")
5+
6+
def main(name, age):
7+
happy_birthday(name, age)
8+
9+
if __name__ == "__main__":
10+
main("Joe", 21)
11+
12+
13+

31-functions/return.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# return = statement used to end a function
2+
#. and send a result back to the caller
3+
4+
5+
def add(x,y):
6+
z = x + y
7+
return z
8+
9+
def subtract(x,y):
10+
z = x - y
11+
return z
12+
13+
def multiply(x,y):
14+
z = x * y
15+
return z
16+
17+
18+
def divide(x,y):
19+
z = x / y
20+
return z
21+
22+
print(add(1,2))
23+
print(subtract(1,2))
24+
print(multiply(1,2))
25+
print(divide(1,2))

32-default-arguments/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# default arguments = A default value for certain parameters
2+
# default is used when that argument is omitted
3+
# make your functions more flexible, reduce # of arguments
4+
# 1. positional, 2. DEFAULT, 3. keyword, 4. arbitrary

0 commit comments

Comments
 (0)