Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if clause fails
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Reply
#6
Make sure it's an int if you want to do number comparisons. With strings, '5' < '15' is False.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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?
Reply
#8
Reading from files is generally done as strings. You can use int() to convert the strings to numbers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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.
Reply
#10
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use the LIKE clause in Python Columbo 9 1,576 Oct-09-2022, 10:22 PM
Last Post: Larz60+
  SQL Query is not executing WHERE clause hammer 7 2,273 Nov-15-2021, 01:44 PM
Last Post: hammer
  How does this if clause work? Pedroski55 3 2,228 Jun-10-2021, 06:31 AM
Last Post: Gribouillis
  can i raise an exception in a try clause? Skaperen 14 5,604 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  pass captured value from input() to where clause metro17 5 3,207 Sep-09-2019, 05:24 AM
Last Post: metro17
  finally clause Skaperen 6 3,815 Jun-02-2019, 09:02 PM
Last Post: snippsat
  how to code in Python "where" clause of SAS FelixS 2 2,746 Mar-26-2019, 04:59 PM
Last Post: FelixS
  break Statements and else Clause on Loops Phytonnewbie 2 2,755 Mar-02-2018, 09:50 AM
Last Post: buran
  My else clause doesn't work (general help) NMW 10 7,829 Jul-17-2017, 01:07 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020