I started taking the Dr Angela Yu Python class on Udemy few days ago and I've got a question regarding her "Love Calculator" code :
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
combined_names = name1 name2
lower_names = combined_names.lower()
t = lower_names.count("t")
r = lower_names.count("r")
u = lower_names.count("u")
e = lower_names.count("e")
first_digit = t r u e
l = lower_names.count("l")
o = lower_names.count("o")
v = lower_names.count("v")
e = lower_names.count("e")
second_digit = l o v e
score = int(str(first_digit) str(second_digit))
print(score)
The consol prints out the result below :
Welcome to the Love Calculator!
What is your name?
True Love
What is their name?
True Love
1010
I'd like to understand why the result of print(score)
is 1010
and not 88
as there are only 4 characters in each words.
Thank you very much for your help :)
CodePudding user response:
It is because of the letter e
which is counted twice for each of the "True Love" inputs because there is an e
in true and in love.
So instead of each character being counted once, you have 3 of them counted once and 1 counted twice, which gives 5 count
for each word. Since the phrase is repeated, it then becomes 10 counts
per word, and the string "10" added to "10" is "1010" and converting that to an integer we get 1010
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n") # True Love
name2 = input("What is their name? \n") # True Love
combined_names = name1 name2 # True LoveTrue Love
lower_names = combined_names.lower() #true lovetrue love
t = lower_names.count("t") # 2
r = lower_names.count("r") # 2
u = lower_names.count("u") # 2
e = lower_names.count("e") # 4
first_digit = t r u e # 10
l = lower_names.count("l") # 2
o = lower_names.count("o") # 2
v = lower_names.count("v") # 2
e = lower_names.count("e") # 4
second_digit = l o v e # 10
score = int(str(first_digit) str(second_digit)) # "10" "10"
print(score) # 1010
CodePudding user response:
I took your code and added in some additional print statements to illustrate why the value comes out as "1010".
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
combined_names = name1 name2
print("Combined Names:", combined_names)
lower_names = combined_names.lower()
t = lower_names.count("t")
r = lower_names.count("r")
u = lower_names.count("u")
e = lower_names.count("e")
first_digit = t r u e
print("First Digit:", first_digit)
l = lower_names.count("l")
o = lower_names.count("o")
v = lower_names.count("v")
e = lower_names.count("e")
second_digit = l o v e
print("Second Digit:", second_digit)
score = int(str(first_digit) str(second_digit))
print(score)
When I run that program and enter in "True Love" for both names, this is the resulting output on the terminal.
@Una:~/Python_Programs/Love$ python3 Love.py
Welcome to the Love Calculator!
What is your name?
True Love
What is their name?
True Love
Combined Names: True LoveTrue Love
First Digit: 10
Second Digit: 10
1010
First off, the entered names are concatenated into a string, "True LoveTrue Love". That string is sampled and the counts are built for the first digit and the second digit. For the first digit, two "t's", two "r's", two "u's" and four "e's" are found in that concatenated string. Therefore the first digit field will contain a value of "10" (2 2 2 4). Likewise, for the second digit calculation, two "l's", two "o's", two "v's", and four "e's" are found in the concatenated string. So again, the sum of those counts is "10". Finally, the two digits are converted to strings and then concatenated. This results in the string value "1010".
So if this calculator program is supposed to derive a different answer, you need to circle back and reevaluate your code.