1
\$\begingroup\$

My love calculator gives a result from 0 to 100% based on these factors:

  1. If the first letters of the names are the same.
  2. If the number of vowels is the same.
  3. If the length of the name is the same.
  4. If the number of consonants is the same.

Also, there is an extra boost of 10-50%, so the result is also partially as a result of luck. Can the coding below be simplified or improved to function more smoothly?:

(Edit 1: By the way, I'm very new at Python - I joined the community only yesterday.)

(Edit 2: I am using Python 3.7.3 for Mac OSX.)

name1 = input("Please type Name 1.\n")
name2 = input("Please type Name 2.\n")

total_vowel1 =0
for i in ['a','e','i','o','u']:
     total_vowel1 += name1.count(i)
total_vowel2 =0
for i in ['a','e','i','o','u']:
     total_vowel2 += name2.count(i)

love = 0
if(total_vowel1 == total_vowel2):
    import random
    love +=random.randint(10,30)

consonants1 = 0
consonants2 = 0
CONSONANTS = 'bcdfghjklmnprstvwxyz'
consonants1 = len([letter for letter in name1 if letter.lower() in 
CONSONANTS])
consonants2 = len([letter for letter in name2 if letter.lower() in 
CONSONANTS])

if(consonants1 == consonants2):
    import random
    love +=random.randint(20,40)

line1 = name1
line2 = name2
split1 = line1.split()
split2 = line2.split()
fl1 = [word[0] for word in split1]
fl2 = [word[0] for word in split2]

if (fl1 == fl2):
    import random
    love +=random.randint(10,30)

if (len(name1) == len(name2)):
    import random
    love+=random.randint(1,10)

import random
love +=random.randint(10,50)
if (love>100):
    love = 100

print("Calculating...")
import time
import random
time.sleep(random.randint(1,3))

print("",name1,"and",name2,"have a",love,"% relationship.")
if ((love>90) or (love == 90)):
    print("They have an unbreakable relationship that will last 
    forever.")
if ((love<89) or (love == 89)) and ((love>70) or (love == 70)):
    print("They have a strong relationship that will most likely 
    lead to a marriage.")
if ((love<69) or (love == 69)) and ((love>50) or (love == 50)):
    print("They have a good relationship that can lead to a 
    honeymoon to Paris.")
if ((love<49) or (love == 49)):
    print("They have a weak relationship that could have been a 
    'match made in heaven'.")
\$\endgroup\$
  • \$\begingroup\$ Feels like a beginner python program. Please edit if that is not the case here. \$\endgroup\$ – hjpotter92 Apr 5 at 20:15
  • \$\begingroup\$ Also, please mention the python version you're using. python-3 or python-2? \$\endgroup\$ – hjpotter92 Apr 5 at 20:16
1
\$\begingroup\$

First, your definition of consonants can be simplified a bit using set operations:

from string import ascii_lowercase

# Storing vowels in a set instead of a list
vowels = {'a', 'e', 'i', 'o', 'u'} 

# Read as "Take the set of lowercase letters, and remove the vowels"
consonants = set(ascii_lowercase) ^ vowels

The vowel counting code below that is repetitious. You have the same counting loop twice. This is a good place to make use of a function:

def count_vowels(name):
    count = 0
    for i in vowels:
        count += name.count(i)

    return count

total_vowel1 = count_vowels(name1)
total_vowel2 = count_vowels(name2)

Instead of importing random a few times throughout the script, just import it at the top once. Unless you have a good reason to hold off on importing something until you really need it, just import any modules right at the top for clarity.


Your spacing is kind of a mess. At different points you have:

  • total_vowel1 =0
  • consonants1 = 0
  • love +=random.randint(10,50)
  • ((love<49) or (love == 49))
  • love +=random.randint(10,30)
  • love+=random.randint(1,10)

See the problem? You're either being wildly inconsistent in your spacing, or you aren't caring to make sure your code readable and nice looking. Pick a spacing style and stick to it. I'd recommend putting spaces around infix operators (love < 49 or love == 49).


You're using two different counting and naming methods for vowels and consonants: a full loop, and a comprehension:

total_vowel1 =0
for i in ['a','e','i','o','u']:
     total_vowel1 += name1.count(i)

. . .

consonants1 = len([letter for letter in name1 if letter.lower() in consonants])

Again, try to be consistent. You or someone else may need to read your code later, and consistency greatly helps readability.


((love>90) or (love == 90)) is needlessly complicated. Just write love >= 90.


In that same area, you have a bunch of ifs. They're all exclusive of each other though, so really, all the ifs after the first should be elifs.


if love <= 89 and love >= 70 (which I fixed up) could be simply written as 70 <= love <= 89. Python, unlike most languages, allows for comparison chaining.


The last case can just be an else. By elimination, if the other cases weren't picked, the last one must be.



After taking all that into consideration and fixing some more spacing, I ended up with:

from string import ascii_lowercase
import random
import time

name1 = input("Please type Name 1.\n")
name2 = input("Please type Name 2.\n")

vowels = {'a', 'e', 'i', 'o', 'u'}
consonants = set(ascii_lowercase) ^ vowels

def count_vowels(name):
    count = 0
    for i in vowels:
        count += name2.count(i)

    return count

total_vowel1 = count_vowels(name1)
total_vowel2 = count_vowels(name2)

love = 0
if(total_vowel1 == total_vowel2):
    love += random.randint(10, 30)

consonants1 = len([letter for letter in name1 if letter.lower() in consonants])
consonants2 = len([letter for letter in name2 if letter.lower() in consonants])

if(consonants1 == consonants2):
    love += random.randint(20, 40)

line1 = name1
line2 = name2
split1 = line1.split()
split2 = line2.split()
fl1 = [word[0] for word in split1]
fl2 = [word[0] for word in split2]

if (fl1 == fl2):
    love += random.randint(10,30)

if (len(name1) == len(name2)):
    love += random.randint(1,10)

love += random.randint(10,50)

if (love > 100):
    love = 100

print("Calculating...")

print(name1, "and", name2, "have a", love, "% relationship.")

if love >= 90:
    print("They have an unbreakable relationship that will last forever.")

elif 70 <= love <= 89:
    print("They have a strong relationship that will most likely lead to a marriage.")

elif 50 <= love <= 69:
    print("They have a good relationship that can lead to a honeymoon to Paris.")

else:
    print("They have a weak relationship that could have been a 'match made in heaven'.")
\$\endgroup\$
  • \$\begingroup\$ Solid advice. I've been using python for quite some time and I didn't know you could comparison chaining. My life is changed for the better! \$\endgroup\$ – loganjones16 Apr 5 at 21:21
  • \$\begingroup\$ @loganjones16 It's very handy. Unfortunately, the only languages that I know that support it are Python and Clojure (although Clojure's syntax is a little different because it's a lisp: (<= 1 x 4)). \$\endgroup\$ – Carcigenicate Apr 5 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.