Python Forum

Full Version: Love Calculator - If and Else
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am making a love calculator using python. I am going to use a points based system to calculate the 'Love Percentage' So I have a list of criteria;
Both first names have the same character length.[+20]
Both first names start with a vowel.[+10]
Both first names start with a consonant [+5]
Both names have the same number of vowels [+12]
Both names have the same number of consonants [+12]

I dont need answers to all of them all I need is help to work out first names that start with vowels and then I was thinking of putting the consonants in a else and vowels in a if? However my code keeps saying that the string has vowels when it does not? 
here is my code so far...

POINTS = 0
CON = 0
VOL = 0
print ("Welcome to the love calculator")
Love1 = input("Please enter the first persons name\n")
Love2 = input("Please enter the second persons name\n")

L1 = (len(Love1))
L2 = (len(Love2))

S1 = (Love1[:1])
S2 = (Love2[:1])

if L1 == L2:
    POINTS = POINTS + 20

if S1 == "a" or "e" or "i" or "o" or "u":
    POINTS = POINTS + 5
else:
    POINTS = POINTS + 2.5
if S2 == "a" or "e" or "i" or "o" or "u":
    POINTS = POINTS + 5
else:
    POINTS = POINTS + 2.5

print (POINTS)
Oh thank you, I literally spent two hours of my lesson today trying to work it out and i only needed brackets :)
You're really close! The issue is with how the if condition is written for checking vowels. In Python, this line:

if S1 == "a" or "e" or "i" or "o" or "u":
does not do what you expect. It actually always evaluates to True, because "e" (and so on) are non-empty strings, which are always truthy in Python.

✅ Correct way to check if a character is a vowel:
You should use the in keyword to check if the first character is in the string of vowels:

if S1.lower() in "aeiou":
    POINTS += 5
else:
    POINTS += 2.5

if S2.lower() in "aeiou":
    POINTS += 5
else:
    POINTS += 2.5
This checks if the first letter (in lowercase) is one of the vowels.
use instead:
if S1 in "aAeEiIoOuU':
    print("S1 is a vowel")