-2

I was trying to create a function with no experience in Python, it was an exercise I found on a website and this was the exercise:

"Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.

Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren't."

I tried everything and went through tons of errors but at the end it didn't work

flower1 = input("Number of petals in the first flower: ")
flower2 = input("Number of petals in the second flower: ")

def lovefunc( flower1, flower2 ):
    
    flower1 = int(flower1)
    flower2 = int(flower2)
    
    lovetest = flower1 + flower2
    lovetest /= 2
    if isinstance(lovetest, float): lovetest = "yes"
    else: lovetest = "no"
    
    for lovetest in "yes": print ("You are in love")
    else: print ("You aren't in love")
    
print(lovefunc(flower1, flower2))

And it doesn't matter the input this is always the output:

You are in love You are in love You are in love You aren't in love None

What did I do wrong?

Pasqui
  • 1
  • 1
    When you divide, you will always get a float. – 12944qwerty Feb 13 '23 at 14:10
  • you cannot use "else" if there is not an "if" first – Sergio2405 Feb 13 '23 at 14:12
  • Does this answer your question? [Check if a number is odd or even in Python](https://stackoverflow.com/questions/21837208/check-if-a-number-is-odd-or-even-in-python) – 001 Feb 13 '23 at 14:12
  • @Sergio2405 Python does have [`for/else`](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops). – 001 Feb 13 '23 at 14:14
  • @Sergio2405 `else` can be used with a `for` loop and it's executed as a final step if the loop does not encounter a `break` statement. – Chillie Feb 13 '23 at 14:14
  • Welcome to Stack Overflow. In your own words, where the code says `for lovetest in "yes":`, what do you think this means? In particular, why does it say `for`? What do you expect that to do? In the future, please carefully read code before posting, and try the advice in https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ to find simple problems. – Karl Knechtel Feb 13 '23 at 14:15
  • Please also read: [What is the purpose of the return statement? How is it different from printing?](https://stackoverflow.com/questions/7129285/) – Karl Knechtel Feb 13 '23 at 14:16
  • There are multiple problems with the code in this post, and no single clear *question* - so it is not appropriate for Stack Overflow. Please read [ask] and note well that this is **not a discussion forum**, and that we do not provide a debugging service. – Karl Knechtel Feb 13 '23 at 14:17

3 Answers3

1

What's wrong with:

flower1 = input("Number of petals in the first flower: ")
flower2 = input("Number of petals in the second flower: ")

def lovefunc(flower1, flower2):
    if (flower1 + flower2) % 2 == 1:
        print("You are in love")
        return True
    else:
        print("You aren't in love")
        return False

result = lovefunc(flower1, flower2)

The result of division is always a float, and you have some messy conditions afterwards, that is why you are getting this output.

Notice I also removed the last print, because the function is already doing the printing so you only need to invoke it. The result (True/False) will be saved in the result variable.

sagi
  • 40,026
  • 6
  • 59
  • 84
  • It worked but now the output is: You aren't in love None – Pasqui Feb 13 '23 at 14:14
  • Oh that makes sense I didn't know I could just call a function like that. Thank you very much! – Pasqui Feb 13 '23 at 14:20
  • What do you mean? That was the goal – Pasqui Feb 13 '23 at 14:21
  • 1
    @JohnnyMopp Yea missed that. – sagi Feb 13 '23 at 14:23
  • If you mean I had to print true and false it doesn't really matter ahah – Pasqui Feb 13 '23 at 14:24
  • Last question, if I put this on codewars.com with Python 3.10 it gives me this error: Traceback (most recent call last): File "/workspace/default/tests.py", line 2, in from solution import lovefunc File "/workspace/default/solution.py", line 1, in flower1 = input("Number of petals in the first flower: ") EOFError: EOF when reading a line – Pasqui Feb 13 '23 at 14:25
  • @Pasqui this should be a separate question – 12944qwerty Feb 13 '23 at 16:13
0

It looks to me like you are checking to see that the data type is a float after dividing your "lovetest" variable by 2. You can try using the modulus operator instead.

lovetest = flower1 + flower2
if (lovetest % 2 == 0):
    lovetest = "No"
else:
    lovetest = "Yes"

By doing this you are checking the remainder when dividing "lovetest" by 2. If it is an even number the remainder will equal zero. Else if it is an odd number the remainder will be 1.

0

The solution was:

def lovefunc(flower1, flower2):
if (int(flower1) + int(flower2)) % 2 == 1:
    return True
else:
    return False
Pasqui
  • 1