Skip to content
Snippets Groups Projects
Commit e78b9686 authored by Daniel Moix's avatar Daniel Moix
Browse files

Lecture 3 Files

parent a45c80a4
Branches
No related tags found
No related merge requests found
# Any Imports
import random
# Slide 10
# Write a function generateUsername() which takes the
# user's first, middle, and last names. A username is
# in the format abc1234, where the three letters are
# the person's first, middle, and last initial followed
# by a four-digit random integer
def generateUsername(first, middle, last):
return random.randint(1000, 9999)
# Slide 37
# Write a function isValidTriangle() which takes a list
# of three positive float values representing the lengths
# of the sides. Using the info on Slide 37, decide whether
# these three sides can be arranged to form a triangle
def isValidTriangle(sides):
return sides[0] > 0
if __name__ == "__main__":
username = generateUsername("Anthony", "Joseph", "Drexel")
print(username)
lengths = [8.6, 7.5, 3.0]
isTriangle = isValidTriangle(lengths)
print(isTriangle)
\ No newline at end of file
grades = [ float(input(f"Enter grade {i+1}: ")) for i in range(0,4)]
average = sum(grades) / len(grades)
print(f"Average grade: {average:.1f}")
\ No newline at end of file
print("Hello Lecture 3!")
name = "Moix"
longer = "This is a longer string of text"
#0123456789012345678901234567890
print(name)
print(len(name))
print(len("A literal string"))
print(name[1:3])
print(longer[ 1 : 10 : 2 ])
print(len(longer[ 1 : 10 : 2 ]))
print(3 + 2)
print("three" + "two")
print(str(1) + "three")
digits = 98.6
print(float(int(float("99789.87"))))
print(int(digits))
print(digits)
print(float("3e8"))
print(ord('🐉'))
print(chr(128009 + 1))
print("She called him \"interesting\" the other day")
print('She called him "interesting" the other day')
print("Isn't is a \"contraction\"")
print("A\nB")
price = 5
item = "coffee"
print(item + ":\t" + str(price))
print("coffee:\t%.2f" % price)
print(f"{item}:\t{price:.2f}")
names = ["you", "Carol", "Bob"]
names[0] = "anthony"
print(names.index("Bob"))
print([f"{i} squared is {i**2}" for i in range(1, 10)])
set1 = {"A", "B", "C"}
set2 = {"C", "A", "B", 7}
print(set1)
print(set2)
print(set1 == set2)
print(7 in set1)
print(7 in set2)
\ No newline at end of file
states = { "AZ" : "Arizona",
"CA" : "California",
"PA" : "Pennsylvania",
"OTH" : ["Washington DC", "Puerto Rico"]
}
print(states["OTH"])
# del states["AZ"]
print(states)
class EL:
def __init__(self, inches=0, feet=0, yards=0):
self.__inches = inches % 12
self.__feet = feet + inches // 12
self.__yards = yards + self.__feet // 3
self.__feet = self.__feet % 3
def getInches(self):
return self.__inches
def getFeet(self):
return self.__feet
def getYards(self):
return self.__yards
def totalInches(self):
return self.__inches + self.__feet * 12 + self.__yards * 36
def addInches(self, additionalInches):
newTotalInches = self.totalInches() + additionalInches
tempLength = EL(newTotalInches)
self.__yards = tempLength.getYards()
self.__feet = tempLength.getFeet()
self.__inches = tempLength.getInches()
def __add__(self, otherEL):
newEL = EL(self.totalInches() + otherEL.totalInches())
return newEL
def __mul__(self, intValue):
return EL(intValue * self.totalInches())
def __eq__(self, otherEL):
return self.totalInches() == otherEL.totalInches()
def __lt__(self, otherEL):
return self.totalInches() < other.totalInches()
def __le__(self, otherEL):
return self.totalInches() <= other.totalInches()
def __ge__(self, otherEL):
return self.totalInches() >= other.totalInches()
def __gt__(self, otherEL):
return self.totalInches() > other.totalInches()
def __ne__(self, otherEL):
return not self == otherEL
def __getitem__(self, item):
if item == 0:
return self.__inches
elif item == 1:
return self.__feet
elif item == 2:
return self.__yards
else:
raise Exception("Bad Index")
def __str__(self):
return f"An English Length object of {self.__inches} inches, {self.__feet} feet, and {self.__yards} yards."
if __name__ == "__main__":
lenA = EL(11)
lenB = EL(9)
lenC = lenA + lenB
print(lenC.getInches(), "inches")
print(lenC.getFeet(), "feet")
print(lenC.getYards(), "yards")
print(lenC.totalInches(), "inches total")
print(lenA != lenB)
print(lenC)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment