3
class cga(object):
    ''''''
    def __int__(self,i,o):
        ''''''
        self.i = i
        self.o = o

    def get(self):
        ''''''
        self.i = []
        c = raw_input("How many courses you have enrolled in this semester?:")
        cout = 0
        while cout < c:
            n = raw_input("plz enter your course code:")
            w = raw_input("plz enter your course weight:")
            g = raw_input("plz enter your course grade:")
            cout += 1 
            self.i.append([n,w,g])
if __name__ == "__main__":
    test = cga()
    test.get()

my problem is the if i type 5 when program ask how many courses i enroll. The loop will not stop, program will keep asking input the course code weight grade. I debugged when it shows program has count cout = 6, but it doest compare with c and while loop does not stop.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
robin
  • 31
  • 1
  • 1
    First, fix your indentation. Not all of your code seems to be formatted properly. Second, add print statements (or functions) to show the values of `cout` and `c` so we can see what's happening. – S.Lott May 05 '11 at 19:31
  • 3
    You know, if you don't want to use docstrings, you can leave them out instead of simply putting a blank one in there. – bgw May 05 '11 at 19:36
  • 1
    Please tag this as `homework` if it's homework. – Mu Mind May 05 '11 at 21:12

2 Answers2

6

The problem is, that raw_input returns a string (not a number), and for some odd historical reasons, strings can be compared (for ordering) to any other kind of object by default, but the results are... odd.

Python 2.6.5 (r265:79063, Oct 28 2010, 20:56:23) 
[GCC 4.5.0 20100604 [gcc-4_5-branch revision 160292]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 < "2"
True
>>> 1 < "0"
True
>>> 1 < ""
True
>>> 

Convert the result to an integer before comparing it:

c = int(raw_input("How many courses you have enrolled in this semester?:"))
Dirk
  • 30,623
  • 8
  • 82
  • 102
  • 2
    "for some odd historical reasons"? Not really. It's a perfectly sensible reason: "Explicit is better than Implicit". An implicit conversion is the wrong thing to do. – S.Lott May 05 '11 at 19:38
  • The error should be made *explicit* by raising an exception rather than *implicitly* seeming to work but producing counter-intuitive results. The fact that it does this is indeed a historical artifact. – kindall May 05 '11 at 20:02
  • 1
    @S.Lott: what's explicit about this behavior? Wouldn't it be more explicit to raise a `TypeError`? – Eli Bendersky May 05 '11 at 20:07
  • @Eli Bendersky: "Wouldn't it be more explicit to raise a `TypeError`"? Apparently not. It appears that it's more explicit to compare the objects and return `False`. – S.Lott May 05 '11 at 20:21
  • @S.Lott: Firstly, the comparison returns `True` (!) in Py2. Note, that the behaviour has been dropped (and rightly so in my opinion) in Py3 in favour of a `TypeError`: the above example yields `TypeError: unorderable types: int() < str()` – Dirk May 05 '11 at 20:39
  • @Dirk: Excellent point. I was only suggesting that the historical reason isn't "odd", there was some sense to it. It may not be the very *best* sense, but it wasn't random, either. – S.Lott May 05 '11 at 20:40
2

raw_input returns a string, not an int. Your evaluation logic is flawed. You'll have to check whether the user input a valid value (positive integer, presumably less than some maximum value allowed). Once you validate this, then you'll have to cast c to an int:

c=int(c)

Only then will your comparison logic work how you expect.

AJ.
  • 27,586
  • 18
  • 84
  • 94