Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 23, 2021 11:02 pm GMT

Match made in Python.

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 TRUELOVE 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? 
")name2 = input("What is their name?
")

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 + name2combined_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_countl_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 out 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!


Original Link: https://dev.to/jdc1492/match-made-in-python-bl5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To