PROBLEM SET 1: DEEP THOUGHT | SOLUTION (CS50 PYTHON)

preview_player
Показать описание


––– DISCLAIMER –––

The following videos are for educational purposes only. Cheating or any other activities are highly discouraged!! Using another person’s code breaks the academic honesty guidelines. This solution is for those who have finished the problem sets and want to watch for educational purposes, learning experience, and exploring alternative ways to approach problems and is NOT meant for those actively doing the problem sets. All problem sets presented in this video are owned by Harvard University.

–––
Рекомендации по теме
Комментарии
Автор

Cool video! This is the code that I did and it worked as well:

answer = input("What is the Answer to the Great Question of Life, the Universe, and Everything? ").lower().strip()

match answer:
case "42" | "forty-two" | "forty two":
print("Yes")
case _:
print("No")

Instead of using the if and elif, I used the match function because it was shorter

lightS
Автор

I solved this one with the match option, but thanks for giving us another way to solve it. Here's mine.

def main():
answer = input("What is the Answer to the Great Question of Life, the Universe, and Everything? ").lower().strip()

match answer:
case "42" | "forty-two" | "forty two":
print("Yes")
case _:
print("No")


main()

sir_rojas
Автор

nice video. here was my solution after messing around for a bit:

def main():
answer = input("What is the Answer to the Great Question of Life, the Universe, and Everything? ").lower().strip()
check_answer(answer)

def check_answer(x):
if x == "42" or x == "forty two" or x == "forty-two":
print("Yes")
else:
print("No")

main()

Clay
Автор

Here is mine, but now i realise i could of made it shorter with case matching

user = input("What is the answer to the greatest question of Life, the Universe, and Everything? ").lower().strip()




if user == "42":
print("Yes")

elif user == "forty-two":
print("Yes")

elif user == "forty two":
print("Yes")

else:
print("no")

kieranjackson
Автор

# I first play with the answer and then y verify if the match one of the possible cases.

answ = input("What is the Answer to the Great Question of Life, the Universe and Everything? ")
answ = answ.strip().casefold() # Remove spaces and render lowercases
answ = answ.replace("-", "")

print(answ)


match answ:
case "42" | "forty-two" | "fortytwo":
print("Yes")
case _:
print("No")

MigueRubio
Автор

Thank you very much.. Alternative with fewer lines:

answer = input("What is the answer to the Great Question of Life, the Universe and Everything?")

if answer.strip() == "42" or answer.lower().strip == "forty-two" or answer.lower().strip() == "forty two":
print("Yes")
else:
print("No")

captain_ali_
Автор

I really appreciate how you explain the topic to its basics for each line of code, rather than just saying what you need to do. It helps me think about it logically why I am doing it and makes everything on that specific topic much easier to grasp and understand

Dr.Unsteady
Автор

This worked for me:

# Ask user for input, strip and lowercase
question = input("What is the answer to the great question of life, the universe, and everything? ").lower().strip()

# if else statement
if question == 'forty-two' or question == 'forty two' or question == '42':
print('Yes')
else:
print('No')

thomasbrittinghamjr.
Автор

user_answer = input("What is the Answer to the Great Question of Life, the Universe, and
if user_answer == "42" or user_answer == "forty-two" or user_answer == "forty two":
print("Yes")
else:
print("No")

natigoat-wg
Автор

this also works and its short too

x = input("What is the Answer to the Great Question of life, the Universe and Everything ").lower().strip()
if x == "42" or x == "forty-two" or x == "forty two":
print("Yes.")
else:
print("No.")

AbuzerZia
Автор

or do it with array

list = ["42", "forty two", "forty-two"]

def main():
msg = input("What is the Answer to the Great Question of Life, the Universe, and Everything? ")
msg = msg.lower().strip()
if msg in list:
print("yes")
else:
print("no")

main()

getschwiftypickle
Автор

Great work but rather than using if statement, we can easily achieve this with fewer lines of code using lower(), replace(), strip() and the match statement:

user_input = (input("What is the Answer to the Great Question of Life, the Universe, and Everything? ")).lower().replace("-", " ")

match user_input:
case "42" | "forty two":
print("Yes")
case _:
print("No")

ankito-
Автор

Giovanna you are amazing! I'm really learning with your videos, thanks you for your time in making the videos and helping many people who is starting their journey on Python

marcopuruncajas
Автор

def main():
x= input (" what is the answer to the great wuestion of life, the universe, and everything? " ).lower().strip()
if (x == "42" or "forty-two" or "Forty two"):
print(" Yes")
else:
print( " NO ")

main()

chiranjibigautam
Автор

I made a shorter way to solve the problem

the_answer = str(input("What is the Answer to the Great Question of Life, the Universe, and Everything? " )).strip()
if the_answer.lower() in ["forty two", "forty-two", "42"]:
print("Yes")
else:
print("No")

restilestari
Автор

I really appreciated your effort. I have been looking up to your videos for help though this particular one I did it myself. But again thank you :)

adarshgurung
Автор

Hey! Thank you for the content, it is very helpful. I just have a small question regarding the answer. Why could it not have been
if answer == "42" or "forty-two" or "forty two"
print("Yes")
else:
print("No")
?
I have tried to use this method but unfortunately it did not work and I fail to understand why. Thank you in advance

mtndmb
Автор

Good explanation, feeling like a true programmer now xD!

Coltwhyte
Автор

answer = input("What's the answer to the great question of life? ")
match answer:
case "42" | "forty-two" | "forty two":
print("Yes!")
case _:
print("No!")

justwalk
Автор

An easy and optimized code for this problem

# Taking Input From the User
user_input = input("What is the Answer to the Great Question of Life, the Universe, and Everything? ")

# Converting the user input into upper case letters for dealing with case sensitive letters
user = user_input.upper()

# Comparing the user input with the following statements
if user == 'FORTY-TWO' or user == 'FORTY TWO' or user == '42':

#If matches with any of the statements. Yes will be printed
print("Yes")
else:

# If the if statement is false then the programme outputs No
print("No")

# Credits :- S.Moulali
print("This Code is Written by S.Moulali")

Last_Hope