-1

Started learning python a few days ago and was doing a task with simple yes/no inputs and doing different things with the while loop depending no whether the user wants to continue using the program or not.

The whole program is fairly small so hope it's alright to post the entirety of its code here. This is what worked for me:

import random

print("=====================================")
print("I Add Numbers and Tell You the Sum v2")
print("=====================================")

while True:
    rand1 = random.randint(1, 100)
    rand2 = random.randint(1, 100)
    result = rand1 + rand2

    print(f"I'm adding {rand1} and {rand2}. If you add them together,the result would be {result}")
    print()

    again_input = input("Would you like to try again? (Y/N) ")
    again = again_input.lower().strip()

    validyes = ["yes", "y"]
    validno = ["no", "n"]

    if again in validyes:
        print("Sure thing!")
        print()
    elif again in validno:
        print("Okay. See you!")
        break
    else:
        print("That's not a valid response. The program will now exit.")
        break

While the relevant code that didn't work as expected was this, to do with checking the user input against the valid list:

valid = ["yes", "y", "no", "n"]

if valid == "yes" or "y":
    print("Sure thing")
elif valid == "no" or "n":
    print("Okay bye")
    break
else:
    print("That's not a valid response. The program will now exit")
    break

The former would run just fine, while the latter will print "Sure thing" regardless of what the user inputs. Why is that the case?

On that front, I'm happy to hear any other tips you guys might have with regards to making the rest of the code better. Eager to hear from and take part in this community!

Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
owlgohoot
  • 9
  • 1

5 Answers5

1

This is how or works
operation: x or y
result: if x is false, then y, else x

Explanation: valid == "yes" will be false for obvious reason because you are comparing a list to a string. When the first condition is false operator or goes to evaluate next condition which is just "y" and will always be true(you can confirm it using bool("y")) so that's why it's always printing "Sure thing".

Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
1

In the second case you're using the OR operand wrong and that's why it is always printing Sure thing. Here you can take a look and understand it better.

To make the code better, i would suggest keeping the valid list with all valid inputs, and checking for yes or no using the if again in valid method, but playing with what is and isn't valid inputs.

1

You have to show what all the string what they are comparing to

if again == "yes" or again == "y":
    print("Sure thing")
elif again == "no" or again == "n":
    print("Okay bye")
    break
else:
    print("That's not a valid response. The program will now exit")
    break

Also a tip is to use "\n" for a new line. The \n will not be shown

Old:

print(f"I'm adding {rand1} and {rand2}. If you add them together,the result would be {result}")
print()

New:

print(f"I'm adding {rand1} and {rand2}. If you add them together,the result would be {result}\n")

Last is that for the lower and strip function u can use it in the same line were u get your input

Old:

again_input = input("Would you like to try again? (Y/N) ")
again = again_input.lower().strip()

New:

again = input("Would you like to try again? (Y/N) ").lower().strip()
Cube
  • 48
  • 4
0

You use this:

if valid == "yes" or "y":
    print("Sure thing")
elif valid == "no" or "n":
    print("Okay bye")
    break

So in the first condition you check if (valid == "yes") or ("y"), but not if valid == ("yes" or "y"). Non-empty string is always True, when you use it as bool, so the first condition is always True. If you want to do somwthing like this, you can use tuples (it's like lists, but you cant edit it): if valid in ("yes", "y")

diduk001
  • 202
  • 1
  • 11
  • @Austin If you want to say that valid is list, so it's question's author mistake, not mine, it's just easier to compare my version with question. Else I can't understand you – diduk001 Jul 18 '20 at 15:56
0

valid is a list, so valid will never equal to "yes", so it just goes to "y", which will always equal true. You need to check if "yes" or "y" is in valid:

if "yes" in valid or "y" in valid:
    print("Sure thing")
elif "no" in valid or "n" in valid:
    print("Okay bye")
    break

Of course, with this code it will always print "Sure thing" because valid includes all the options.

F.M
  • 1,369
  • 1
  • 16
  • 31