Python Forum
if clause fails - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: if clause fails (/thread-17338.html)

Pages: 1 2


if clause fails - DeadCthulhuWaitsDreaming - Apr-07-2019

OK, this shouldn't be that hard but maybe I'm missing something.

This is the full function, the problem lies between the ###########################################################################s.
If anyone wants to see the full code I'll upload, but it seems pretty clear to me it's not the issue, but hell I'm a self taught coder so maybe I'm wrong. Someone please let me know if I'm doing something wrong or if it's a stupid bug and I should redo it in another coding language. Also I'm using Python 2.7.1 and the Idle IDE for windows 10.

Edit: Noticed a strange indentation in that exact section after uploading code, gonna go fix that quick.

def GetCards():

    commonList = [];
    uncommonList = [];
    rareList = [];
    common1 = '';
    common2 = '';
    common3 = '';
    common4 = '';
    common5 = '';
    uncommon1 = '';
    uncommon2 = '';
    uncommon3 = '';
    rare = '';
    commonSet1 = '';
    commonSet2 = '';
    commonSet3 = '';
    commonSet4 = '';
    commonSet5 = '';
    uncommonSet1 = '';
    uncommonSet2 = '';
    uncommonSet3 = '';
    rareSet = '';
    complete = 0;
    commonPrice1 = 0.00;
    commonPrice2 = 0.00;
    commonPrice3 = 0.00;
    commonPrice4 = 0.00;
    commonPrice5 = 0.00;
    uncommonPrice1 = 0.00;
    uncommonPrice2 = 0.00;
    uncommonPrice3 = 0.00;
    rarePrice = 0.00;
    maxValue = 0.00;
    packValue = 0.00;
	
	
    while complete != 2:
        
        complete = 0;
        
        #need to find how to get a username and match it to a name in a database
        #need to check if user has any spins before enabling the spin button
        #need to attach a paypal payment so user can add 1-2 more spins
	collection = importCollection();
	
	for i in range(10):
###########################################################################################################################################
            print(collection[i].qty);  <---- This outputs the correct values
            if collection[i].qty < 5: <---- This doesn't care
                print(len(collection)); <---- Never prints regardless of collection[i].qty
##########################################################################################################################################
                #for j in range(i + 1, len(collection)):
                    #collection[j - 1] = collection[j];
                    #collection = collection[:1];

        print(len(collection));                
        lngListCounter = 0
        for lngRecordCounter in range(len(collection)):
            if collection[lngRecordCounter].rarity == 'C':
                commonList.append(collection[lngRecordCounter]);
                lngListCounter += 1;
        lngListCounter = 0
        for lngRecordCounter in range(len(collection)):
            if collection[lngRecordCounter].rarity == 'U':
                uncommonList.append(collection[lngRecordCounter]);
                lngListCounter += 1;
        lngListCounter = 0
        for lngRecordCounter in range(len(collection)):
            if collection[lngRecordCounter].rarity == 'R' or collection[lngRecordCounter].rarity == 'M':
                rareList.append(collection[lngRecordCounter]);
                lngListCounter += 1;

        print(commonList[2].name);
        print(len(commonList));
        print(uncommonList[2].name);
        print(len(uncommonList));
        print(rareList[2].name);
        print(len(rareList));
        complete = 2;

Well I tried a change that should have done it but...
for i in range(10):
		  num = collection[i].qty;
		  print(num); <---- Again it prints the correct value so there is no uncertainty of what the if clause is looking for
		  if num < 5: <---- Using num which has a set value still fails to trigger the if clause
			print(len(collection)); <---- Still never prints



RE: if clause fails - Yoriz - Apr-07-2019

From the information shown there is no way of knowing what collection[i].qty is and what it returns.
what does print(collection[i].qty) return.
what type does print(type(collection[i].qty)) return.
you should be able to isolate out all the other code and just make a small sample code of the area in question.


RE: if clause fails - DeadCthulhuWaitsDreaming - Apr-07-2019

I'm printing num out so that I know what it's value is when it passes through the if clause, 8 out of 11 values printed out are less than 5. As far as I know there is nothing that should effect the value in num before it reaches the if clause, unless I'm wrong num sent into the if is the same value that printed out. Although when num is assigned a direct value like 3 and then tested to see if it's 3 it passes and obviously the print statement prints out all 3s


RE: if clause fails - ichabod801 - Apr-07-2019

Yes, but if you print the string '5' and the int 5, they look the same. That's why Yoriz suggested printing the type, so you can be sure it is an integer.


RE: if clause fails - DeadCthulhuWaitsDreaming - Apr-07-2019

HAHA they're strings, thanks a bunch, I'll either throw some quotes around the number I'm checking against or find where my int becomes a str.


RE: if clause fails - ichabod801 - Apr-07-2019

Make sure it's an int if you want to do number comparisons. With strings, '5' < '15' is False.


RE: if clause fails - DeadCthulhuWaitsDreaming - Apr-07-2019

Ah I see, gotcha. The info is read in from a file, I never changed the string to an int, I guess a typecast should work?


RE: if clause fails - ichabod801 - Apr-07-2019

Reading from files is generally done as strings. You can use int() to convert the strings to numbers.


RE: if clause fails - DeadCthulhuWaitsDreaming - Apr-07-2019

Everywhere I've tried to cast to int I get an error, but I have now just realized there are blank values and that just isn't gonna cast well is it.


RE: if clause fails - ichabod801 - Apr-07-2019

Then you need to decide what you will do with the blank values. The best way to handle this is generally a try/except block:

try:
   value = int(something)
except ValueError:
   # handle blank input.