Тёмный

PROBLEM SET 0: TIP CALCULATOR | SOLUTION (CS50 PYTHON) 

Dors Coding School
Подписаться 19 тыс.
Просмотров 27 тыс.
50% 1

📚 Join the Waitlist for our next Bootcamp in September - codingdors.com/bootcamp
👨‍💻 Learn How to Code with Private Classes - www.codingdors.com/coachingplans
💡 Get Our Solutions and an eBook on "The Top Mistakes People Make when Learning How to Code" - www.codingdors.com/landingpage
--- 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.
---

Кино

Опубликовано:

 

30 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 84   
@DorsCodingSchool
@DorsCodingSchool Год назад
👨‍💻 Learn How to Code with Private Classes - www.dorscodingschool.com/coachingplans ❓Having a hard time with CS50, FreeCodeCamp or Odin Project? Practice with our exclusive free coding platform: www.codingdors.com/ 🎯 Are You A Coding Expert? Take Our Free Quiz and Find Out - www.dorscodingschool.com/quiz
@gaurav._.
@gaurav._. 6 месяцев назад
This is how did it. def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(d): # TODO a = float(d.removeprefix('$')) return a def percent_to_float(p): # TODO b = float(p.removesuffix('%'))*float('0.01') return b main()
@arididomenico6974
@arididomenico6974 2 года назад
this problem literally made me want to cry because i tried *everything* i could think of and it wasn't working. turns out i was missing "return".
@sidrahareem3808
@sidrahareem3808 11 месяцев назад
Man you ain't the only one who cried...I had a batsuit crazy mental breakdown because I still don't understand the video and using _ inbetween words confuses the hell out of me but in the end it worked out through a different way somehow 😂
@mohammedali6338
@mohammedali6338 11 месяцев назад
bro i forgot return asw😐
@muhammedqasim25
@muhammedqasim25 5 месяцев назад
same hereeee 😭😭😭
@pranavsangwan
@pranavsangwan 2 месяца назад
mann i was thinking that return might work and i didnt tried amd im like fuckkkkkk.
@therealcryss
@therealcryss Год назад
I was still confused about the structure of functions and parameters as well as how to start the first line, so giving me only that hint from your video helped me a lot. I was struggling for quite a while until I finally decided to get a hint on how to solve this. Thanks!
@chiema9510
@chiema9510 2 года назад
Thanks! Nice to get a review after doing it. Feels like having a TA.
@alejandroaponte276
@alejandroaponte276 9 месяцев назад
Love the way you explain step by step. Thank you very much!
@jolenekearse7512
@jolenekearse7512 2 года назад
Really great descriptive video! The step-by-step walkthrough with examples was so helpful!
@DorsCodingSchool
@DorsCodingSchool 2 года назад
Thank you!
@SDSFDSF631
@SDSFDSF631 4 дня назад
def tip(): use = (input("how much was ur meal? ")) user = float(use.replace('$', '')) top = (input("how much would u like to tip? ")) tp = float(top.replace("%", "")) a = user * tp/100 print(f"leave ${a:.2f}") tip()
@sir_rojas
@sir_rojas 2 года назад
Thanks for the solution. Here is the code I wrote and worked for me. def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(dollars): dollars = dollars.replace("$","") dollars = float(dollars) return dollars def percent_to_float(percent): percent = percent.replace("%","") percent = float(percent) percent = percent / 100 return percent main()
@DorsCodingSchool
@DorsCodingSchool 2 года назад
Thank you! Great job!
@clebex3833
@clebex3833 11 месяцев назад
I'm loving it! Very Helpful
@ArghyaDas793
@ArghyaDas793 11 месяцев назад
Beautifully explained. Thank you for the explanation
@emmanuelmunshya4953
@emmanuelmunshya4953 3 месяца назад
Thank you so much, very clear and coincise explanation.
@yuyum8436
@yuyum8436 5 месяцев назад
This is how I did it. I realize I should have did p = float(p)/100 instead of int :( def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(d): d = d.strip("$") return float(d) def percent_to_float(p): p = p.strip("%") p = int(p)/100 return float(p)
@muratozdemir1365
@muratozdemir1365 2 года назад
I was struggling with the return , thank you for the guidance
@DorsCodingSchool
@DorsCodingSchool 2 года назад
You're welcome!
@bahacelik9740
@bahacelik9740 2 года назад
couldnt thought of that return... thanks for the help!
@DorsCodingSchool
@DorsCodingSchool 2 года назад
You're welcome! By the way, we have a Telegram Group to help people learn how to code and where you can connect with learners from all over the world. Over there you can ask any question about programming and you can easily get your questions answered within 24 hours. Besides that, you will have access to our membership with more than 400 problems made by us to ease your learning curve and 1 hour of group coaching every month! Check www.dorscodingschool.com/products
@trenbolone9586
@trenbolone9586 3 месяца назад
i knew that i needed to use return i couldnt figure it out how to use so igot made
@Qubit313
@Qubit313 11 месяцев назад
Thankyou so much!
@tanishq1239
@tanishq1239 2 года назад
return funtion was pretty tricky for me, thanks
@DorsCodingSchool
@DorsCodingSchool 2 года назад
You're welcome!
@ariesdm1981
@ariesdm1981 9 месяцев назад
lstrip & rstrip works for me 👌👍
@samsadri4108
@samsadri4108 Год назад
thank you so much this videos is so usefull
@zAngus
@zAngus Год назад
Good to see how others have solved it. Its Week 0 and I often find it hard and have to re-watch the lectures and look for clues in his examples. My approach was: def main(): dollars = dollars_to_float(input("How much was the meal? ").replace("$","")) percent = percent_to_float(input("What percentage would you like to tip? ").replace("%","")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(d): return float(d) def percent_to_float(p): return float(p) / 100 main()
@DorsCodingSchool
@DorsCodingSchool Год назад
Great job!
@dominiquewashington9441
@dominiquewashington9441 Год назад
@@DorsCodingSchool Bro that was the chat gpt soloution becasue I got the same exact solution lol
@palmo34
@palmo34 11 месяцев назад
appreciate you posting this answer! I didnt even think to put the .replace in the main function. here's my method for any future people coming through, I believe i simply enjoy having the code more spread out like this, easier to read; although I do think my randomly chosen variables (z,ten,eleven) are unnecessarily confusing: def dollars_to_float(d): ten=d.replace("$","") return float(ten) def percent_to_float(p): eleven=p.replace("%","") z=int(eleven) * .01 return float(z)
@carlosmauriciopm4706
@carlosmauriciopm4706 2 месяца назад
Mi error solo fue no ponerle luego del float el parentesis :/
@imparainglesepianopiano8009
@imparainglesepianopiano8009 2 года назад
Thanks for the video! Good content, keep it up! If I may, I'd like to give you a correction regarding your English - the use of word Replace - we replace something with something else. E.g. Replace A with B. or, Change A to B. :) Hope I didn't sound rude - just wanted to help a tiny bit to make your content even better!
@DorsCodingSchool
@DorsCodingSchool 2 года назад
It wasn't rude. I appreciate your message! Thank you for helping me (:
@lukasschubarth1141
@lukasschubarth1141 3 месяца назад
was really struggling when the only thing that i missed was p2 = float(p1) / 100 my version was with parentheses around the 100 also...
@studywithshin2205
@studywithshin2205 10 дней назад
Here's my answer. # define main() def main(): # call a function and assign var and ask user for input dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) # calculate a var called tip by multiplying dollar with percentage tip = dollars * percent # print var called tip as f str with .00 print(f"Leave ${tip:.2f}") # because we haven't defined the two functions above, we need to define them first def dollars_to_float(d): #remove $ from str and turn x into a float x = d.replace("$" , "") return float(x) # define percent_to_float() def percent_to_float(p): # remove % from str and turn it into a float and divide by 100 y = p.replace("%" , "") y1 = float(y) return y1/100 main() I couldn't solve it for hours and left it for three days. I came back and solved in 15 mins! I was overcomplicating things and overlooking the fact that input() can only take str and two different str cannot be multiplied. When I realized that, viola! I start asking the questions! How do I remove $ % from the str and turn them into float. I think the biggest challenge and fun part of being a programmer is knowing what how to ask the right questions. For me, I break them down with # and start my logical thinking step by step. I know no better; I'm just a rookie.
@alexbrijosebastian4381
@alexbrijosebastian4381 2 года назад
Thank you so much for this
@DorsCodingSchool
@DorsCodingSchool 2 года назад
You're welcome!
@brucebergkamp
@brucebergkamp Год назад
was totally stuck on percent_to_float... was trying to cramp everything into a singline line of code. this is what i had ===> return float(p//100.replace('%', ' ')) and i kept getting TypeError: unsupported operand type(s) for /: 'str' and 'int'... your video saved me. thx
@DorsCodingSchool
@DorsCodingSchool Год назад
You're welcome! Did you know you can join our Free Discord Group and get help from me and from people around the world that are also learning how to code? dorscodingschool.com/discord
@swayamsatish2460
@swayamsatish2460 Год назад
Does lstrip and rstrip not work for this problem?
@oliverlopez3191
@oliverlopez3191 8 месяцев назад
i couldnt for the life of me understand how to replace the $ and % signs. i didnt realize we had to use a function ive never heard of lol, i tried to stick to the lectures only. at least i understood how to return the float. i also thought it was silly to just not add the "/100" to the original equation of dollar * percent. i feel like that makes the calculator easier to understand than defining it with the percent function, but i suppose thats a part of the assignment. just in case i added /100 to the end of the return percent function and it worked.
@amon2059
@amon2059 2 года назад
you are a wizard!
@DorsCodingSchool
@DorsCodingSchool 2 года назад
Thank you (:
@battlingmyself2268
@battlingmyself2268 11 дней назад
How she pass d and p while both were not defined?
@bgsebs
@bgsebs Год назад
I used the remove prefix and remove sufix instead replace, it worked hahaa so i did not toch it
@matteroftime8333
@matteroftime8333 2 года назад
Thanks for your teaching. I just have a simple question that dollar_to_float(d): the (d) is a varible refering whatever user input in this case?
@DorsCodingSchool
@DorsCodingSchool 2 года назад
The variable d will be whatever the user passes as a parameter to this function. If you want, we can explain this further in a free 30-minute session: www.dorscodingschool.com/schedulemeeting
@katrinascherben3022
@katrinascherben3022 Год назад
Why is there a variable in the defining function? For example, "def dollars_to_float(d): " Where did the "(d)" come from? Why is it there?
@DorsCodingSchool
@DorsCodingSchool Год назад
D is a parameter that we are passing when we call the function, which will be the amount of dollars that the user will type in :)
@paulq0246
@paulq0246 Год назад
Can someone explain to me what this means? print(f"Leave ${tip:.2f}") I understand the print function and the"Leave $" and somewhat the {tip} but I don't understand the {:. 2f} ??? I know it's a f string but I have zero idea why this is in there, could someone help me please?
@DorsCodingSchool
@DorsCodingSchool Год назад
2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter. If you want our help, you can join our Free Discord Community: www.dorscodingschool.com/discord
@bretthart1499
@bretthart1499 6 месяцев назад
I think David Malan should take the backseat and let you drive. I learn more from you!
@richardpuusaag1296
@richardpuusaag1296 10 месяцев назад
Good to see what solutions other people came up with. I for example, used strip instead of replace: def dollars_to_float(d): without_dollar = d.strip("$") return float(without_dollar) def percent_to_float(p): without_percentage = p.strip("%") return float(without_percentage) / 100
@Vincent2103
@Vincent2103 4 месяца назад
is this allowed? i feel like im cheating.. cant help but search for guide lol
@kasnow9556
@kasnow9556 Год назад
Can i ask you something? there is one thing that confuses me , why are you inserting the "(d)" in the def function, instead of just dollars. because both of them works. so i am a little bit confused about that by the way i love you content you make it look so easy! :)
@JARVPT
@JARVPT 9 месяцев назад
I assume it is because it's written as "def dollars_to_float(d):" in line 8 so it has to do with that "(d)"
@LaidoStrike
@LaidoStrike 7 месяцев назад
When you define a new function, you can call the new arguments as you'd like to. In the "scope" section, David said that it is up to each functions to name its own variables or arguments
@timjames4306
@timjames4306 2 года назад
Plz post project 1 also🙏🏻
@DorsCodingSchool
@DorsCodingSchool 2 года назад
We will (:
@khoon9484
@khoon9484 2 года назад
I executed your instructions completely correctly, but in the 4th line, it gives an error that the tip is not float where is the problem from ?
@DorsCodingSchool
@DorsCodingSchool 2 года назад
In your line 11 and 17 are you converting the return values of the functons into a float? By the way, we have a Telegram Group to help people learn how to code and where you can connect with learners from all over the world. Over there you can ask any question about programming and you can easily get your questions answered within 24 hours. Besides that, you will have access to our membership with more than 400 problems made by us to ease your learning curve and 1 hour of group coaching every month! Check www.dorscodingschool.com/products
@raymondnduka1954
@raymondnduka1954 Год назад
Here's the code that I wrote but for some reason an error pops up saying couldn't convert string to float. I would like your help and also an explanation on why this error keeps on showing up, if it's not to much to ask. Thanks def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(d): # TODO d = d.replace("$","") float(d) return d def percent_to_float(p): # TODO p = float(p.replace("%","")) p = p / 100 return p main()
@andrec3152
@andrec3152 Год назад
Make d=float(d)
@palmo34
@palmo34 11 месяцев назад
im pretty sure your issue was that you had the variables on the left of the equal the same as the variables with .replace and the to_float functions. my method was more convoluted then necessary but it worked: def dollars_to_float(d): ten=d.replace("$","") return float(ten) def percent_to_float(p): eleven=p.replace("%","") z=int(eleven) * .01 return float(z) ten and eleven were just random words i chose for variables In hindsight i could have done it in fewer lines of code but honestly i feel better having it be more spread out and easier to read.
@abbymayuri6486
@abbymayuri6486 Год назад
but the dollar sign was not removed?
@DorsCodingSchool
@DorsCodingSchool Год назад
It was
@doaflamingo3713
@doaflamingo3713 2 года назад
def dollars_to_float(d): return float(d.replace('$', '')) def percent_to_float(p): return float(p.replace('%', '')) def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent / 100 print(f"Leave ${tip:.2f}") main() this has worked so far. The only thing i did differently, is NOT dividing by 100 in float, but at end in the main
@DorsCodingSchool
@DorsCodingSchool 2 года назад
Great job!
@andrejg3086
@andrejg3086 11 месяцев назад
My code: def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(d): return float(d[1:]) def percent_to_float(p): return float(p[:-1]) / 100 main()
@user-eg5cg3lk3e
@user-eg5cg3lk3e 2 года назад
Ahh. Nice. Pretty clean. Didn't think about .replace() method. Instead used .removeprefix() and .removesuffix() accordingly. I was wondering how can you implement the same functionality but if the user enteers other currency symbols like yen or euro . I mean you cant type all these signs in parameters.. At the top of my head, I can only think of a loop with a list of all the symbols to check and remove if in list
@DorsCodingSchool
@DorsCodingSchool 2 года назад
It is possible! If you want, we have one Telegram Group to help people learn how to code and we can discuss it. Check www.dorscodingschool.com/products
@dinero2131
@dinero2131 Год назад
One way to go about that is by importing 're' and using the following to disregard any signs other than a number like such... #at the top import re def dollars_to_float(d): d = float(re.search(r'\d+', d).group()) return d def percent_to_float(p): p = float(re.search(r'\d+', p).group()) p = p / 100 return p You can wrap an int or float around (re.search(r'\d+', VALUE).group())
@phayden0
@phayden0 2 года назад
Boa , aqui no meu eu utilizei o .strip() pra retirar o "$" e o "%" ! Alguma preferencia ?
@DorsCodingSchool
@DorsCodingSchool 2 года назад
É uma ótima forma de se resolver! A solução pode ser feita de diversas formas, acabei optando por fazer de outro modo (:
@mathswithbuka5365
@mathswithbuka5365 2 года назад
Nice I used .strip() method for this as it was the fastest
@user-eg5cg3lk3e
@user-eg5cg3lk3e 2 года назад
Ahh. Nice. Pretty clean. Didn't think about .replace() method. Instead used .removeprefix() and .removesuffix() accordingly. I was wondering how can you implement the same functionality but if the user enteers other currency symbols like yen or euro . I mean you cant type all these signs in parameters.. At the top of my head, I can only think of a loop with a list of all the symbols to check and remove if in list
@DorsCodingSchool
@DorsCodingSchool 2 года назад
We replied to you in your other comment ;)
@user-eg5cg3lk3e
@user-eg5cg3lk3e 2 года назад
@@DorsCodingSchool I see, I didnt post this one. Some YT bug I guess :)
@chiranthgowda8386
@chiranthgowda8386 2 года назад
u could use rstrip and lstrip functions , which takes out any character in the left or right
Далее
PROBLEM SET 1: DEEP THOUGHT | SOLUTION (CS50 PYTHON)
13:28
PROBLEM SET 0: INDOOR VOICE | SOLUTION (CS50 PYTHON)
7:02
CS50P PSet0 ~ Tip Calculator | Solution
5:25
Просмотров 1,5 тыс.
Learn Python Object Oriented Programming! 🚗
12:18
Просмотров 14 тыс.
5 Useful Dunder Methods In Python
16:10
Просмотров 56 тыс.
PROBLEM SET 6: CS50 P-SHIRT | SOLUTION (CS50 PYTHON)
19:12
Live🔴 girls funny😂Chinese
0:31
Просмотров 7 млн
бим бам бум💥💥 типа..
0:18
Просмотров 6 млн