Lab 10-Done

This commit is contained in:
Edwin Hui
2023-11-03 21:58:49 -07:00
parent 8fa578984b
commit 2649ecd376
3 changed files with 76 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
# Edwin Hui
import random
my_list = ['earth', 'day', 'laptop', 'blue', 'up']
# defining functions
def printhello():
print("hello from my function")
def count20():
@@ -13,9 +14,12 @@ def sort():
my_list.sort()
print(my_list, end = " ")
print("----------------")
# prints hello from my function
printhello()
# prints 1 to 20
count20()
print(" ")
# prints random number multiplied by 7
multiply()
# prints my_list sorted
sort()

View File

@@ -1,18 +1,22 @@
# Edwin Hui
import random
accept = [4, 6, 8, 10, 12, 20]
def dice():
print(random.randint(1, 6))
dice()
# Automatically rolls a 6 sided dice
# ----------------
def dice_1():
return random.randint(1, 6)
print(dice_1())
# does the same as above, but with a return statement
# ----------------
def dice_2(num):
return random.randint(1,num)
num = int(input("Enter a number: "))
print(dice_2(num))
# rools a dice with a number of sides based on user input
# ----------------
num = 0
def dice_3(num):
@@ -23,8 +27,12 @@ def dice_3(num):
return random.randint(1,num)
num = input("Enter a number: ")
print(dice_3(num))
# rolls a dice with a number of sides based on user input
print("No input:", dice_3(''))
# Demos what happens when no input is given
# ----------------
num = 0
# checks if the number of sides is valid, if not, sets it to 6
def checkSides(sides):
if sides in accept:
num = int(sides)
@@ -33,14 +41,20 @@ def checkSides(sides):
print("Invalid number of sides, using 6 sides")
num = 6
return num
# rolls a dice with a number of sides based on user input
def dice_4(sides):
num = checkSides(sides)
return random.randint(1,num)
sides = int(input("Enter a number: "))
# prints the result of the dice roll
print(dice_4(sides))
# Demos what happens when an invalid input is given
print("Non-Valid Input:", dice_4('7'))
# ----------------
num = 0
sides = int(input("Enter a number of sides: "))
# it asks for how many times to roll now too!
rolls = int(input("Enter a number of rolls: "))
def checkSides(sides):
if sides in accept:
num = int(sides)
@@ -52,14 +66,8 @@ def checkSides(sides):
def dice_5(sides, rolls):
num = checkSides(sides)
number = []
for x in range(0, rolls):
return print(random.randint(1,num))
sides = int(input("Enter a number of sides: "))
rolls = int(input("Enter a number of rolls: "))
number.append(random.randint(1,num))
print(number)
dice_5(sides, rolls)

52
lab10/lab10-3.py Normal file
View File

@@ -0,0 +1,52 @@
# Edwin Hui
import random
accept = [4, 6, 8, 10, 12, 20]
number = []
# checks if the number of sides is valid, if not, sets it to 6
def checkSides(sides):
if sides in accept:
num = int(sides)
return num
else:
print("Invalid number of sides, using 6 sides")
num = 6
return num
# rolls a dice with a number of sides based on user input
def dice(sides, rolls):
num = checkSides(sides)
number = []
for x in range(0, rolls):
number.append(random.randint(1,num))
return number
# asks for how many sides and how many times to roll
def ask():
global sides
global rolls
sides = int(input("Enter a number of sides: "))
rolls = int(input("Enter a number of rolls: "))
return sides, rolls
# loops the dice rolling
def loop():
global sides
global rolls
global number
print("--------------------------------")
number = dice(sides, rolls)
while len(number) > 0:
print(number[0], end=" ")
number.pop(0)
print("\n--------------------------------")
pram = input("Press Enter to re-roll, q to quit, or n to set new parameters->")
if pram == "q":
# quits the program
print("Goodbye")
elif pram == "":
# rerolls with the same parameters
loop()
elif pram == "n":
# lets you set new parameters
sides = int(input("Enter a number of sides: "))
rolls = int(input("Enter a number of rolls: "))
loop()
ask()
loop()