Author Topic: [Python] MIT 6.00sc Problem set 2 woes  (Read 1681 times)

0 Members and 4 Guests are viewing this topic.

Offline Sparky712

  • Peasant
  • *
  • Posts: 117
  • Cookies: 14
    • View Profile
[Python] MIT 6.00sc Problem set 2 woes
« on: August 09, 2013, 08:28:29 pm »
well, recently, i have been following the MIT OCW for computer science, starting with 6.00. the videos are going ok, and i've been taking loads of notes. however, it's come to problem set 2, and I cannot get the program to run. No doubt I've done something stupid, but I just can't see the syntax error.

Code: (python) [Select]
oBalance = raw_input("Please input your outstanding credit card balance: $")
air = raw_input("Please input your annual credit card interest rate as a decimal: ")
mmpr = raw_input("Please enter your minimum monthly payment rate as a decimal: ")

currentMonth = 1

x = 0
for x in range(0,12)
    print "Month: " + currentMonth
    currentMonth = currentMonth + 1
    mmp = round(mmpr * oBalance, 2)
    interestPaid = round((air/12) * oBalance, 2)
    principalPaid = round(oBalance - principalPaid, 2)
    oBalance = round(oBalance - principalPaid, 2)

    print "Minimum monthly payment: $" + mmp
    print "Principal paid: $" + principalPaid
    print "Remaining Balance: $" + oBalance

« Last Edit: August 09, 2013, 08:33:54 pm by Sparky712 »

Offline Thor

  • Serf
  • *
  • Posts: 29
  • Cookies: 15
  • whoami?
    • View Profile
Re: [Python] MIT 6.00sc Problem set 2 woes
« Reply #1 on: August 09, 2013, 08:36:45 pm »
Lines 7 and 8 look to be the problem.

When using "for x in range(0,10)" you don't have to give x a value before hand, so you don't need line 7 at all.

Whilst inside the loop, x will take the value of whatever iteration you are on. So in the first loop it will take the value 0, then in the next loop 1, and this will continue up to 9 then exit the loop.

The problem you have here is that  you haven't specified anything to do in the loop. Nothing is happening here.
You need to put some code in the loop to execute on each iteration.


Try this
Code: [Select]
for x in range(0,10):    # Notice the colon also
    print x

-- EDIT --

You changed your code whilst I was posting that :P

The first thing I'm noticing here is that you're missing a colon at the end of  "for x in range(0,12)"
Put a colon at the end and it should work.
« Last Edit: August 09, 2013, 08:40:19 pm by Thor »
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.

Offline Sparky712

  • Peasant
  • *
  • Posts: 117
  • Cookies: 14
    • View Profile
Re: [Python] MIT 6.00sc Problem set 2 woes
« Reply #2 on: August 09, 2013, 08:37:56 pm »
look again Thor, looks like you saw this when i accidentally posted early
however, it may be the colon.

Ok, now it works. now, I just have a contatenation problem. >.>' I need to try and make that int a string or something?
« Last Edit: August 09, 2013, 08:40:33 pm by Sparky712 »

Offline Thor

  • Serf
  • *
  • Posts: 29
  • Cookies: 15
  • whoami?
    • View Profile
Re: [Python] MIT 6.00sc Problem set 2 woes
« Reply #3 on: August 09, 2013, 08:48:04 pm »
look again Thor, looks like you saw this when i accidentally posted early
however, it may be the colon.

Ok, now it works. now, I just have a contatenation problem. >.>' I need to try and make that int a string or something?

That's simple enough.
print "Month: " + str(currentMonth)

str(int) converts the int to a string.


Another thing I'm noticing is this
Code: [Select]
principalPaid = round(oBalance - principalPaid, 2)

You are using the variable principalPaid before you have declared what it is. You have to initiate it with a value first, for example principalPaid = 0, and put that outside of the loop so that it doesn't get reset to 0 on every iteration.
« Last Edit: August 09, 2013, 08:50:47 pm by Thor »
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.

Offline Sparky712

  • Peasant
  • *
  • Posts: 117
  • Cookies: 14
    • View Profile
Re: [Python] MIT 6.00sc Problem set 2 woes
« Reply #4 on: August 09, 2013, 08:57:15 pm »
oh no, that's just a clerical error. i am using this laptop for internet, my computer for programming. had to type it over into this one. that's me rereading a bit by accident. the program works fine now i've sorted the contatenation thing out. must try and keep that in mind.

Thank you for your help.
Have a cookie.
« Last Edit: August 09, 2013, 08:58:00 pm by Sparky712 »