This week in python, I made a "Love Calculator". The program takes the input of two names and counts the amount of times the letters T,R,U,E,L,O,V, and E appear in both names. TRUE will add up to one score and LOVE will add up to another. At this point we want to combine the two digits together to make a larger number. An example could be if both name inputs amount to 5 for true and 9 for love the score together would become 59.
The first thing needed is to use the input functions for both names. We have them set to variables of name1 and name2. After the user inputs the names, the names will then be set to the variables rather than the input method expecting some sort of input on the users behalf.
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
One way to cut down on a few lines of code came from the idea of concatenating both names into a combined string. from there, we used the lower() function to turn all the letters in the string into lowercase letters.
combined_str = name1 + name2
combined_str_lwr = combined_str.lower()
After this line we then begin to count the times each letter appears in the string using the count() method on the string. The function can take up to three arguments, but in this case we only want to check for one letter at a time which results in the count method being used 8 times against the combined string also setting them to their own individual variable. An example would be to find all the 't's in the combined string we would set the count of the combined string with and argument of the letter 't' to the variable of t_count. This would happen for every letter in the phrase TRUELOVE.
t_count = combined_str_lwr.count("t")
r_count = combined_str_lwr.count("r")
u_count = combined_str_lwr.count("u")
e_count = combined_str_lwr.count("e")
true = t_count + r_count + u_count + e_count
l_count = combined_str_lwr.count("l")
o_count = combined_str_lwr.count("o")
v_count = combined_str_lwr.count("v")
e_count = combined_str_lwr.count("e")
love = l_count + o_count + v_count + e_count
We also will add up the totals for each word. So true would have a count equal to an integer and the same for the variable love.
score = f"{true}{love}"
scr_int = int(score)
if scr_int < 10 or scr_int > 90:
print(f"Your score is {scr_int}, you go together like coke and mentos.")
elif scr_int > 40 and scr_int < 50:
print(f"Your score is {scr_int}, you are alright together.")
else:
print(f"Your score is {scr_int}.")
Using an f string we combine true love as a string which will result in our score. Afterwards, we turn that score into a variable. That integer is then used in our if, else conditional statements that will print out a certain message to the user depending on which of the conditionals returns true. This was a pretty fun project to complete. While I'm going through the content of this udemy course slowly, I'm still working through it and constantly learning something new. I can't wait to get even deeper with this stuff.
Take time to learn something new this week and as always...Happy Coding!
Top comments (4)
Hello there!
I am totally new to coding and this is my first course. I got up to the challenge and I COULD not figure it out! How in the heck did you figure it out? I am feeling a little lost.
Hey @donswarwick ,
It's been a while since I did this project. One word of advice I have is to not be afraid to go to the next video and see how the course instructor completed the challenge. Chances are, you're on the right track but just got stuck on an important detail that you may have not been able to think of a solution for yet. It also helps, if you're a beginner, to see how more seasoned devs use their logic while you're still developing your own logical programming practice. Sorry for the late response.
OH gosh, no worries at all, I just so pleased you had time to respond! I have found myself like right on the edge a couple of times and could not figure it out, I have spent days trying ot figure some of it out - and usually get it or peek just a tad at her code or stack overflow...but as of recent, espically with loops and day 6 and day 7 I have been lost. I can see my logic is there to a degree but my implemenaion is poor. Would it be better to try and figure it out over days using stack overflow and other Google stuff - or should i just push forward when I hit a block and watch her? I have bought a few other intro courses so I cold go to one of those after I complete hers then come back? I just want to learn.
From my personal experience with the course(Which I still haven't finished, HAHA!), I do what I can and If I don't want the full solution I just a peak over the road block I watch up to what I've done. After getting over that hurdle...I go back to trying it out on my own.
The beautiful thing I'm learning about programming is that while you may have had a hard time getting over a certain concept...others that may have found it a bit easier are usually willing to assist you if you ask.
Check out exercism.org/ . Its a great website that would be an awesome aid to your udemy learning and they offer mentoring for the problems if things aren't clicking the way you'd like.