CS50P Problem Set 1 Solution

Deep Thought

def main():
answer = input("What is the Great Question of Life, the Universe and Everything? " ).strip().lower()
if answer == '42':
print("Yes")
elif answer == 'forty-two':
print("Yes")
elif answer == 'forty two':
print("Yes")
else:
print("No")

if __name__ == "__main__":
main()

Home Federal Savings Bank

def main():

greeting = input("Greetings: ").strip().lower()
if greeting.startswith('hello'):
print('$0')
elif greeting.startswith('h'):
print('$20')
else:
print('$100')

if __name__ == "__main__":
main()

File Extensions

def main():

media = input("Please Enter media name: ").strip().lower().split('.')
media_type = 'application/octet-stream'
if len(media)>1:
if media[-1] in ['png', 'gif']:
media_type = f'image/{media[-1]}'
elif media[-1] in ['jpeg', 'jpg']:
media_type = 'image/jpeg'
elif media[-1] == 'txt':
media_type = 'text/plain'
elif media[-1] in ['pdf','zip']:
media_type = f'application/{media[-1]}'

print(media_type)
main()

Math Interpreter

def main():

expression = input("Expression: ")
math(expression)

def math(e):
x, y, z = e.split(' ')
x = int(x)
z = int(z)
if y == '+':
result = x + z
elif y == '-':
result = x - z
elif y == '*':
result = x * z
elif y == '/':
if z == 0:
print('z cannot be 0')
else:
result = x / z
print(f'{result:.1f}')
if __name__ == '__main__':
main()

Meal Time

def main():

t = input("Enter Time: ")
time = convert(t)
if 7<=time<=8:
print("breakfast time")
elif 12<=time<=13:
print("lunch time")
elif 18<=time<=19:
print("dinner time")

def convert(time):
time = time.split(":")
time1 = float(time[0])
time2 = float(time[1])/60
total_time = time1+time2
return total_time

if __name__ == "__main__":
main()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.