0

I'm making a program that gives you different basic operations to solve. If you choose subtraction, it also gives you a choice if negative numbers are allowed or not. If the user says "No", it's supposed to change the variable that determines the minuend to a different number and check if it's greater than the subtrahend, and if it isn't, it tries again.

I've tried using a while loop that keeps regenerating the minuend until it's greater than the subtrahend, I've tried placing after FAT1 (minuend) = random.randint(1,50) > FAT2 (subtrahend) or FAT1 = random.randint(1,50 > FAT2). I've also tried placing it in a function, or making a boolean used in the while loop instead of FAT1 < FAT2, but it keeps not working.

Variables I made throughout (all global):

FAT1 = first number (int)
FAT2 = second number (int)
OPER = the operation the user chooses (string)
NEG = if the user chooses yes or no to negative numbers (string)

Problematic part of the code:

    #No negative numbers
    elif NEG == "NO" or "No" or "no" or "nO" :
        while True :
            FAT1 = random.randint(1,50)
            FAT2 = random.randint(1,50)
            #Here it takes what the user typed and converts it in an int
            TYP = input()
            NUM = int(TYP)
            while FAT1 < FAT2 :
                FAT1 = random.randint(1,50)
            RES = FAT1 - FAT2
            print("What's",FAT1," - ",FAT2)
            if NUM == RES :
                print("Correct!")
            elif NUM != RES :
                print("Wrong! Try again!")

EDIT: As I said in my comment: I get the input before the problem gets made because the user may choose a different operation. This question could be clearer with the whole code, but i didn't feel like putting it all there because it felt redundant Here's the code: pastebin.com/ZfqhY2md

EDIT2: I tried making this dictionary at the beginning (all in one line):

dict = {'Addition': 'Addition', 'Subtraction': 'Subtraction', 'No': 'No', 
'Yes': 'Yes'}

And at the beginning of every if statement i tried this:

if NEG == dict.get('Yes') :

And it works for every suite except the one with No to negative numbers... New full code here: https://pastebin.com/600TpkZe

MaLoLHDX
  • 3
  • 4
  • Hmmm, there's a lot going on here. Why do you get the input *before* you print out the subtraction problem? – Andrew Jaffe Oct 15 '19 at 15:14
  • Welcome to SO. I suggest you clean up the question a bit. **What do you mean by:** "a program that gives you different basic operations to solve" be a little more concrete - or **add more code**. When is OPER used? – DannyDannyDanny Oct 15 '19 at 15:16
  • 1
    Why not `FAT1 = random.randint(FAT2,50)`? – snakecharmerb Oct 15 '19 at 15:22
  • I get the input before the problem gets made because the user may choose a different operation. This question could be clearer with the whole code, but i didn't feel like putting it all there because it felt redundant Here's the code: https://pastebin.com/ZfqhY2md – MaLoLHDX Oct 15 '19 at 15:39
  • I tried FAT1 = random.randint(FAT2,50) , it still doesn't work. – MaLoLHDX Oct 15 '19 at 18:59
  • Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – jasonharper Oct 15 '19 at 19:08

2 Answers2

0

I think the problem may be in your elif condition.

Checking: NEG == "NO" or "No" or "no" or "nO" is going to return True if, and only if, NEG = 'NO'

rather try: elif NEG.upper() == 'NO':

B Man
  • 89
  • 4
  • Actually, the asker's statement is *always* going to be true, because `"No"`, `"no"`, and `"nO"` are all truthy values. – glibdud Oct 15 '19 at 19:12
  • Apologies, you're correct. It'll be unable to produce False. That being said I still doubt whether this is the intended condition. The rest of the code seems to be running fine for me though (given this snippet) – B Man Oct 15 '19 at 19:17
0

I found out this is a duplicate... I made a tuple for any No you might type and check with: (Notup is the list of NOs)

if OPER in Notup :

Then I made tuples for everything else.

MaLoLHDX
  • 3
  • 4