Turn on thread page Beta
    • Thread Starter
    Offline

    18
    ReputationRep:
    i'm using python to complete the pre-release material for 2016. i'm really stuck with it. i've made a start but i'm unable to finish. also, when i enter a weight greater than 10 it gives the message saying its invalid but then it accepts it and i can't see why. the code also means that despite me entering how many parcels i have to enter the values for, it does not stop.
    attached are screenshots of my code and the results, as well as the question. any help will be greatly appreciated!

    thanks
    alicia
    Attached Images
       
    Offline

    18
    ReputationRep:
    (Original post by lisseyjj)
    when i enter a weight greater than 10 it gives the message saying its invalid but then it accepts it and i can't see why.
    Have a look closely at this bit of your code:

    Name:  1.png
Views: 58
Size:  12.3 KB

    Firstly, you're checking whether the parcel weight, and setting the value of valid = 0, in case the parcel weight is outside the range

    As soon as you've done this, you're checking if (total > maxtotal) and then setting the value of valid = 1

    Further down, you're checking if (valid == 1) and printing the line "parcel can be delivered".

    Step back and think about whether you really want to be changing the value of 'valid' when those checks are successful, or whether the only time you want to change the value of 'valid' is when there's a problem.

    Do you really need these lines or could you get rid of these else clauses altogether?
    Code:
    else:
        valid = 1
    The only other thing to think about is - if those 'else' clauses don't exist, when would you set valid = 1? Maybe you only need to set this once, before any of your checks have started.

    the code also means that despite me entering how many parcels i have to enter the values for, it does not stop.
    Have a close look at the condition in your while loop, and what you're doing at the end of that loop:
    Attachment 724162
    So, if number starts out at 2, consider how its value will change after each parcel:

    First parcel:
    Code:
    number = number + 1
    number == 3
    (number > 0) == true
    Second parcel:
    Code:
    number = number + 1
    (number == 4)
     (number > 0) == true
    Third parcel:
    Code:
    number = number + 1
    (number == 4)
     (number > 0) == true
    etc.
    Attached Images
     
    • Thread Starter
    Offline

    18
    ReputationRep:
    (Original post by winterscoming)
    Have a look closely at this bit of your code:

    Name:  1.png
Views: 58
Size:  12.3 KB

    Firstly, you're checking whether the parcel weight, and setting the value of valid = 0, in case the parcel weight is outside the range

    As soon as you've done this, you're checking if (total > maxtotal) and then setting the value of valid = 1

    Further down, you're checking if (valid == 1) and printing the line "parcel can be delivered".

    Step back and think about whether you really want to be changing the value of 'valid' when those checks are successful, or whether the only time you want to change the value of 'valid' is when there's a problem.

    Do you really need these lines or could you get rid of these else clauses altogether?
    Code:
    else:
        valid = 1
    The only other thing to think about is - if those 'else' clauses don't exist, when would you set valid = 1? Maybe you only need to set this once, before any of your checks have started.


    Have a close look at the condition in your while loop, and what you're doing at the end of that loop:
    Attachment 724162
    So, if number starts out at 2, consider how its value will change after each parcel:

    First parcel:
    Code:
    number = number + 1
    number == 3
    (number > 0) == true
    Second parcel:
    Code:
    number = number + 1
    (number == 4)
     (number > 0) == true
    Third parcel:
    Code:
    number = number + 1
    (number == 4)
     (number > 0) == true
    etc.
    thank you so much! massive help thank you! repped.
    Offline

    18
    ReputationRep:
    (Original post by lisseyjj)
    thank you so much! massive help thank you! repped.
    I'm glad that helped! One other thing to ask - has anybody shown you how to use the Python debugger in IDLE?

    The debugger is a really useful tool for problems like this - you can set a breakpoints at places in your code where you might be interested in watching the content of variables, and indeed the overall flow of your program, stepping through the logic line-by-line.

    There's a page on how to use the IDLE debugger here: https://www.cs.uky.edu/~keen/help/de...ial/debug.html

    The 'short version' of this, is that you just need to right-click on a line somewhere in your program (preferably before the place where you think something is going wrong), and choose 'Set Breakpoint', and then tell the debugger in IDLE to run (F5), and keep clicking the 'Step' button to slowly work through your program one line at a time.

    The debugger will show you what's going on - it'll always point to the current line, so that you know which "path" your program is taking through the code, and you'll be able to see your variables change in real time. This should make it a lot easier for you to troubleshoot these kinds of problems - it's a lot easier than staring at the code hoping to spot a mistake!
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: February 11, 2018

University open days

  1. University of Cambridge
    Christ's College Undergraduate
    Wed, 26 Sep '18
  2. Norwich University of the Arts
    Undergraduate Open Days Undergraduate
    Fri, 28 Sep '18
  3. Edge Hill University
    Faculty of Health and Social Care Undergraduate
    Sat, 29 Sep '18
Poll
Which accompaniment is best?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.