PROBLEM SET 1: HOME FEDERAL SAVINGS BANK | 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.

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

This solution is technically wrong even though it passed the check50. In your program if the greeting is phrased something like "oh, its you mr. Newman, hello" it would print "$0" when if fact it should print "$100" since the greeting didn't started with "hello".
The task was to identify if the answer started with "hello" not if the answer contained "hello". Its a sutil but important difference since the code would be quite different.

javierm.rodriguez
Автор

Bro its PROBLEM SET 1, and she is teaching everythin step by step, what a legend.

hardworkingprocrastinator
Автор

My solution

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

KhalidSaadat-qg
Автор

This is my answer

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

space_quartz
Автор

this is the most helpful channel for cs50 courses. It helps to do the problem sets alongside these videos to get a better grasp on ideas, thanks a lot

reinhardtgrove
Автор

just started the class and i love coming here to see different ways of doing the problems! my code looked a little different and more concise but yours seems a little easier to read. i left mine below

#get input from user, remove whitespace, lowercase it
greeting = input("Greeting: ").strip().lower()

#if hello print $0, if start with h print $20, everthing else print $100
if "hello" in greeting:
print("$0")
elif "h" == greeting[0]:
print("$20")
else:
print("$100")

tylerjjackson
Автор

I think the problem with this solution is that, the question asks us to look for hello at the start while this program looks for hello anywhere in the string.

Here's my solution:

print('enter a greeting:')
a = input().lower()
b = a.removeprefix('h')
c = a.removeprefix('hello')

if c == a:
if b == a:
print('$100')
elif b != a:
print('$20')
else:
print ('$0')

akshatsrivastava
Автор

I solved it with:

def main():

greeting = input("Type a greeting: ").lower().strip()

if greeting.startswith("hello"):
print("$0")
elif greeting.startswith("h"):
print("$20")
else:
print("$100")

main()

redacted__
Автор

Thank you for the video, I love watching your solutions after solving mine to see other methods that can be used to solve the same question and learn more,
Here is my code, I used string slicing
userInput = input("Greeting: ").lower().strip()
# To slice into the str starting from index 0 and ending at index 4
if (userInput[0:5]) == "hello":
print("$0")
# To check the beginning of the word
elif (userInput[0:1]) == "h":
print("$20")
else:
print("$100")

moyofadeadesewa
Автор

Looking at line number 7, it checks to see if 'hello' is in new_answer, but that is not the purpose. The instructions say to check if the greeting *starts* with 'hello' and not if 'hello' exists or not. For example, if new_answer is "Wait a minute. Sorry, hello! How can I help?" We see that 'hello' is in new_answer, but it does not start with 'hello', therefore program will still return '$0' and that goes against the instructions. Either way, great video! Thank you.

itsreason
Автор

You do such an amazing job at teaching, the way you explain by teaching us the methods instead of answering is such a great way to learn. Thank you for your content!

saibfps
Автор

great answer.
Another way is to use the startswith() string(method ) or function to check

kolawoleomotosho
Автор

there is a catch in your code the question states "If the greeting starts with “hello”, output $0. If the greeting starts with an “h” (but not “hello”), output $20" the hello should be present at the start of a input. but if the hello is mentioned for example at place variable[4] then still it outputs 0$ it should output 20$.

tejas
Автор

Thank you! I used lstrip() instead as it only asked removing the leading spaces. Also used startswith()

x=input('Greeting: ')
if
print('$0')
...

adilzafar
Автор

Excellent, I understand much better with your teaching 😍😍 Please post the rest of the problem sets.

maede
Автор

Thanks for the content. Here's my solution where I implemented the startswith() function.

def main():
greeting = input("Greeting: ").lower().strip()

if "hello" in greeting:
print("$0")
elif greeting.startswith("h"):
print("$20")
else:
print("$100")


main()

sir_rojas
Автор

I love your solution it is similar to mine sorta. I did it this way. Ignore the f strings its just burned into my brain.

# ask user for input
greet = input('Greeting: ').strip().lower()

# If the word hello is found anywhere in the string then print $0
if 'hello' in greet:
print(f'$0')

# if the char in the 0 place of the input string is h print ($20)
elif greet[0] == 'h':
print(f'$20')

# Otherwise print $100 for any other input
else:
print(f'$100')

mmisahighcaliberround
Автор

Thanks. This helps to understand the question. Much appreciated.

chooselife
Автор

My solution, considering that the greeting must start with "hello" and not just having "hello" in the sentence:

x =

if x.startswith("hello"):
print("$0")
elif x.startswith("h"):
print("$20")
else:
print("$100")

RzOr
Автор

x = input("Greeting: ").lower().strip()

if "hello" in x:
print("$0")
elif "h" in x[0]:
print("$20")
else:
print("$100")

amelraghavan