Home > Back-end >  100 Days of Code - 37 - Love Calculator - What's wrong with my code?
100 Days of Code - 37 - Love Calculator - What's wrong with my code?

Time:01-03

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}.")

CodePudding user response:

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.

CodePudding user response:

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}.")

CodePudding user response:

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.

  • Related