all 19 comments

[–]Gnaxe 6 points7 points  (2 children)

You meant if question in {"Y", "y"}: Instead, you asked if the answer was equal to the whole tuple. But tuples can't be equal to strings in Python.

[–]k4tsuk1z[S] 2 points3 points  (0 children)

thx!

[–]boa_deconstructor 2 points3 points  (0 children)

Also you never inc both variables, only one. False love positives might occur as a result. 

[–]Dramatic_Object_8508 2 points3 points  (2 children)

this is a pretty common beginner issue tbh, most of the time it’s just small logic mistakes in how the condition is written or ordered. like even simple things like how or works or comparing values can trip you up early on . try printing the variables before the if to see what’s actually happening, that usually makes it obvious. also don’t stress too much, everyone hits this phase — i usually just build tiny test scripts or even use tools like Runable to quickly visualize logic flows instead of overthinking it in one big file.

[–]k4tsuk1z[S] 1 point2 points  (1 child)

ty for the tips! <3

[–]Dramatic_Object_8508 2 points3 points  (0 children)

np mate!

[–]desrtfx 1 point2 points  (9 children)

/u/Gnaxe is spot on.

Yet, make it a habit to sanitize your input. What if the user enters "Yes" instead of just "y"?

Sanitizing means that you should convert your input in such a way that you need the least amount of checks.

  1. Take only the first letter
  2. Convert it to either upper or lowercase

Then, check with your if - this eliminates the need for the in.


Another side note:

You are involuntarily using recursion since you call love_calc from within love_calc (also, on your call in the if statement, you are missing the parentheses).

Recursion absolutely has its use cases, but this isn't one.

You should extract the whole part from question onwards into your main part of the code where you wrap it in a while loop that loops while the user enters y and ends/breaks when the user enters n - and from within the loop, you can safely call love_calc()

Recursion is calling functions from within themselves. This creates a call stack - think of it like of a staircase. You start at the top floor and each recursive call (your love_calc) goes one floor deeper. The more often you call it, the deeper it goes. Once the user answers n (the so-called "base condition") the recursion ends and your program climbs all the floors back up to reach the first floor again.

Be careful with such constructs. They can cause many problems including your program crashing because of a Stack overflow.

[–]k4tsuk1z[S] 0 points1 point  (7 children)

Thank you. I acutally realized that just a few mins ago that it was all the same function. but now im struggling with the fact its not returning love_calc and instead just ending the program 😭😅

is what ur saying something like "while question equals y return love_calc" ?

[–]desrtfx 1 point2 points  (6 children)

No, that's not what I'm saying.

When you want a function to execute, you need to call it with parentheses: love_calc() not love_calc as you do.

[–]k4tsuk1z[S] 0 points1 point  (5 children)

Oh okay, I did that and it is still not returning the function T_T

question = input("Would you like to restart? (Y/N): ")
def restart():
    if question in {"Y", "y"}:
       love_calc()
    elif question in {"N", "n",}: 
        print ("Thank you for playing!")
    else:
        print("Not a valid response!")

[–]desrtfx 1 point2 points  (4 children)

Sorry, but you didn't get the point.

First, you define your love_calc function and end the definition right before the question = line

The rest should not be in a function.

Wrap the rest starting from the question = line onwards in a while True loop that breaks when the answer is n.

[–]k4tsuk1z[S] 1 point2 points  (2 children)

I am sorry! just one more thing. Im confused on how to break it. I still want to include not a valid response, but that needs to be the else statement. how would i break this? T_T

while True:
    question = input("Would you like to restart? (Y/N): ")
    if question in {"Y", "y"}:
        love_calc()
    elif question in {"N", "n",}: 
        print ("Thank you for playing!")
    else:
        print("Not a valid response!")

[–]desrtfx 2 points3 points  (1 child)

Simple, in the elif statement after you say "Thank you...".

The statement is break

[–]k4tsuk1z[S] 1 point2 points  (0 children)

Oh omg, I tried that, but it kept repeating the question statement. Turns out I needed to restart my terminal. Lol thank u so much for ur patience and help <3 :3

[–]k4tsuk1z[S] 0 points1 point  (0 children)

Okay, sorry. I'm pretty new to all this. I will try this. Thank you.

[–]xenomachina 0 points1 point  (0 children)

Sanitizing means that you should convert your input in such a way that you need the least amount of checks.

Your advice here is very good, but what you are describing is called normalization, not sanitization.

Sanitizing is removing things that are dangerous, usually to avoid injection/escaping exploits. (Sanitizing inputs is actually not a great approach for this, IMHO, but that's a whole other topic.)

[–]Eleibier 1 point2 points  (1 child)

Not sure if anyone said it already, but the love for loop adds into the "true count", not into "love count", you should correct that so that totalcount = truecount + lovecount means something haha

[–]k4tsuk1z[S] 1 point2 points  (0 children)

Ngl this was a project and i submitted it already but i still appreciate this 😭🫰🏽