'100 Days of Code - 37 - Love Calculator - What's wrong with my code?
Newbie here and I've taken a shot at a Love Calculator using the logic in my brain and here is what I got prior to some learning tweaks from the 100 Days of Code course. I'm stumped as to why I'm getting '21' as my answer if I used the teacher's provided instructional answers of 'Angela Yu' and 'Jack Bauer' as input, as opposed to 53, the correct answer.
Would love some guidance related to the logic. Thanks!
name1_lower = name1.lower()
name2_lower = name2.lower()
t_count = name1_lower.count("t")
r_count = name1_lower.count("r")
u_count = name1_lower.count("u")
e_count = name1_lower.count("e")
true_count = t_count + r_count + u_count + e_count
l_count = name2_lower.count("l")
o_count = name2_lower.count("o")
v_count = name2_lower.count("v")
e2_count = name2_lower.count("e")
love_count = l_count + o_count + v_count + e2_count
total_count = int(str(true_count) + str(love_count))
#I learned to convert the above to string, and then back to integer
# from Dr. Yu, didn't have this initially
# perhaps I need to do the same above to true_count and love_count?
if total_count < 10 or total_count > 90:
print(f"Your score is {total_count}, you go together like coke and mentos.")
elif total_count >= 40 and total_count <= 50:
print(f"Your score is {total_count}, you are alright together.")
else:
print(f"Your score is {total_count}.")
Solution 1:[1]
This is for the day 3 exercise of out of 100 days course https://www.udemy.com/course/100-days-of-code/ which I am also taking. This is an excellent Python course, I am just at day 3 as well.
The concept of counting is from here https://www.buzzfeed.com/ariannarebolini/what-are-the-chances-your-crush-is-actually-your-true-love
The below answer is good, but we haven't covered for
loops yet.
for name in [name1_lower, name2_lower]:
for letter in ["t","r","u","e"]:
true_count += name.count(letter)
for letter in ["l","o","v","e"]:
love_count += name.count(letter)
Below is my code, which works:
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
name1 = name1.lower()
name2 = name2.lower()
name1_t = name1.count('t')
name1_r = name1.count('r')
name1_u = name1.count('u')
name1_e = name1.count('e')
name2_t = name2.count('t')
name2_r = name2.count('r')
name2_u = name2.count('u')
name2_e = name2.count('e')
name1_l = name1.count('l')
name1_o = name1.count('o')
name1_v = name1.count('v')
name1_e = name1.count('e')
name2_l = name2.count('l')
name2_o = name2.count('o')
name2_v = name2.count('v')
name2_e = name2.count('e')
true_score = name1_t + name1_r + name1_u + name1_e + name2_t + name2_r +
name2_u + name2_e
love_score = name1_l + name1_o + name1_v + name1_e + name2_l + name2_o +
name2_v + name2_e
total_score = true_score * 10 + love_score
if total_score < 10 or total_score > 90:
print(f"Your score is {total_score}, you go together like coke and
mentos.")
elif total_score > 40 and total_score < 50:
print(f"Your score is {total_score}, you are alright together.")
else:
print(f"Your score is {total_score}.")
Just looked at instructor's code. She used the below which is smart to save some lines of code. Instead of check for name 1 and name 2 separately, she concatenate them as a string:
combined string =name1 + name2
Also, she converted the score
to str
to concatenate and then back to type int
in order to compare, while I use:
total_score = true_score * 10 + love_score
Thus, there is no need to change to str
then back to int
.
Solution 2:[2]
First to all, remove the f on the print statement and it will compile. Do like this:
if total_count < 10 or total_count > 90:
print("Your score is {total_count}, you go together like coke and mentos.")
elif total_count >= 40 and total_count <= 50:
print("Your score is {total_count}, you are alright together.")
else:
print("Your score is {total_count}.")
.count("A") returns the number of occurrences of the character "A" on a string. So you need to add these counts without converting them to string. total_count =true_count + love_count
Additionally, I think you need to find true and love count on each name, which you are not doing.
Solution 3:[3]
This may get the result you're looking for, instead of iterating through a single name for each word apply both names to each word.
name1 = "Angela Yu"
name2 = "Jack Bauer"
name1_lower = name1.lower()
name2_lower = name2.lower()
true_count, love_count = 0, 0
for name in [name1_lower, name2_lower]:
for letter in ["t","r","u","e"]:
true_count += name.count(letter)
for letter in ["l","o","v","e"]:
love_count += name.count(letter)
total_count = int(str(true_count) + str(love_count))
if total_count < 10 or total_count > 90:
print(f"Your score is {total_count}, you go together like coke and mentos.")
elif total_count >= 40 and total_count <= 50:
print(f"Your score is {total_count}, you are alright together.")
else:
print(f"Your score is {total_count}.")
Solution 4:[4]
name1 = "Angela Yu" name2 = "Jack Bauer"
T occurs 0 times R occurs 1 time U occurs 2 times E occurs 2 times
Total = 5
L occurs 1 time O occurs 0 times V occurs 0 times E occurs 2 times
Total = 3
Love Score = 53
In your solution provided, you are calculating how often letters “true” appear for Angela and “love” appear for Jack which would correctly give you 21. Instead you should calculate how often letters “true” and “love” appear across both names then combine the two numbers to form 53 as detailed above.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeremy Caney |
Solution 2 | |
Solution 3 | |
Solution 4 | t selvyn |