Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Python HMWK Help?

  1. #1

    Joined
    Apr 2020
    Posts
    24
    Thanks
    8
    Thanked
    12/8
    Mentioned
    1 time
    Time Online
    9h 24m
    Avg. Time Online
    N/A

    Python HMWK Help?

    Hi everyone! I am currently taking an intro Python course on Coursera and I'm getting an error for a compute function. I went on the course forums an was confused because other students who successfully ran their code/program did not have this error, yet they had the same line of code as me.

    This is what I have:

    (you need an account to see links)

    I'm a total Python newbie, of course, but I've been learning C# and JavaScript on the side when I have some free time and for whatever reason, Python is just taking a lot longer to stick for me. I greatly appreciate any help in advance! I want to be able to understand what I am doing wrong!

  2. #2
    Saiyan Race
    j03's Avatar
    Joined
    Dec 2011
    Posts
    13,720
    Userbars
    166
    Thanks
    5,906
    Thanked
    33,076/6,608
    DL/UL
    23/36
    Mentioned
    3,867 times
    Time Online
    563d 4h 55m
    Avg. Time Online
    3h 13m
    The error message which is in that black pop-up tells you what's wrong. Line 10:

    Code:
    p = computepay(h*r)
    Should probably be:

    Code:
    p = computepay(h,r)
    You're using an asterisk (*) instead of a comma (,).
    @(you need an account to see links)
    (you need an account to see links)
    (you need an account to see links)(you need an account to see links)

    ------------------------
    [02/24/2013] Stealth CORE is made into the first standalone Neopets auto-player.
    ------------------------


  3. The Following User Says Thank You to j03 For This Useful Post:

    linttastic (05-31-2020)

  4. #3

    Joined
    Apr 2020
    Posts
    24
    Thanks
    8
    Thanked
    12/8
    Mentioned
    1 time
    Time Online
    9h 24m
    Avg. Time Online
    N/A
    (you need an account to see links)

    Syntax errors will be the death of me. I hit some other errors after that one, but fixed them right away and was able to run the code successfully. Many thanks for the help!!!

  5. #4

    Joined
    Apr 2020
    Posts
    24
    Thanks
    8
    Thanked
    12/8
    Mentioned
    1 time
    Time Online
    9h 24m
    Avg. Time Online
    N/A
    Hi all I'm back Can anyone tell me why the output (where it says mismatch) is generating 'None' for the 'Minimum is' string? I thought I had the syntax entered correctly since it's the same as the string for 'Maximum is':

    (you need an account to see links)

    Thank you in advance!

  6. #5

    Joined
    May 2016
    Posts
    217
    Userbars
    9
    Thanks
    437
    Thanked
    229/96
    DL/UL
    17/0
    Mentioned
    18 times
    Time Online
    7d 14h 56m
    Avg. Time Online
    3m
    Quote Originally Posted by linttastic View Post
    Hi all I'm back Can anyone tell me why the output (where it says mismatch) is generating 'None' for the 'Minimum is' string? I thought I had the syntax entered correctly since it's the same as the string for 'Maximum is':

    (you need an account to see links)

    Thank you in advance!
    Hmm there is a structure problem in your Python code, provide another "tab" at the start of line 9 and line 11.

    Try that, maybe that does it!

    EDIT: Also because you are printing that invalid input line, (line 14) you might have another failure later on with the output not matching. Just fyi lol

  7. #6

    Joined
    Apr 2020
    Posts
    24
    Thanks
    8
    Thanked
    12/8
    Mentioned
    1 time
    Time Online
    9h 24m
    Avg. Time Online
    N/A


    ---------- Post added at 10:44 PM ---------- Previous post was at 10:44 PM ----------

    Quote Originally Posted by aaronsd View Post
    Hmm there is a structure problem in your Python code, provide another "tab" at the start of line 9 and line 11.

    Try that, maybe that does it!

    EDIT: Also because you are printing that invalid input line, (line 14) you might have another failure later on with the output not matching. Just fyi lol
    Ok so I tried adding another tab to those lines, but the code didnt run. When I moved it back again to where I had it, I was able to run the code, but still showing 'Minimum is' as none. Is the problem with my line 11 since that's where the 'Minimum is' comes from? It's just weird to me since it looks just the same as the 'Maximum is' string and that one printed correctly

  8. #7

    Joined
    May 2016
    Posts
    217
    Userbars
    9
    Thanks
    437
    Thanked
    229/96
    DL/UL
    17/0
    Mentioned
    18 times
    Time Online
    7d 14h 56m
    Avg. Time Online
    3m
    Quote Originally Posted by linttastic View Post


    ---------- Post added at 10:44 PM ---------- Previous post was at 10:44 PM ----------



    Ok so I tried adding another tab to those lines, but the code didnt run. When I moved it back again to where I had it, I was able to run the code, but still showing 'Minimum is' as none. Is the problem with my line 11 since that's where the 'Minimum is' comes from? It's just weird to me since it looks just the same as the 'Maximum is' string and that one printed correctly
    Okay so there's two big problems here still.

    1) Your TRY and EXCEPT are malformed. What you've done is this:
    Code:
    block 1: TRY
       block 1-1: num = int(num)
    block 2: IF#1
       block 2-1: IF #1 is true, do this
    block 3: IF#2
       block 3-1: IF #2 is true, do this
    block 4: EXCEPT
       block 4-1: EXCEPT for the TRY command before
    You need to move the BLOCK 4 under BLOCK 1 because that EXCEPT is not really registering correctly and your structure is just wrong.

    Two ways to fix the structure are:

    #1
    Code:
    block 1: TRY
       block 1-1: num = int(num)
    block 2: EXCEPT
       block 2-1: EXCEPT for the TRY command before
    block 3: IF#1
       block 2-1: IF #1 is true, do this
    block 4: IF#2
       block 3-1: IF #2 is true, do this
    #2
    Code:
    block 1: TRY
       block 1-1: num = int(num)
       block 1-2: IF#1
          block 1-2-1: IF #1 is true, do this
       block 1-3: IF#2
          block 1-3-1: IF #2 is true, do this
    block 2: EXCEPT
       block 2-1: EXCEPT for the TRY command before

    2) 2nd problem. Different programming languages deal with values "None"/null/""/undefined etc differently. In the case of Python, your IF#2 is never being run, because what's happening is that the value of "None" is always lower than no matter any other value you have. To add on to this a bit more, in Python any positive number value I know for a fact will be registered as a higher "value" than "None" which is why you don't need to explicitly do "largest = num" in the code below. I haven't tested enough to see if a negative number will still register as a higher "value" than "None" though, you'll have to test that and let me know hah.

    So you need to add somewhere after num = int(num) and before your IF#2 the following:

    Code:
       if smallest is None:
          smallest = num

    I am happy to assist with anything more haha, I think if you make these changes your code will be almost there, it should give you the correct result but there are still things in there which I would have done differently
    Last edited by aaronsd; 06-02-2020 at 12:37 AM.

  9. The Following User Says Thank You to aaronsd For This Useful Post:

    linttastic (06-02-2020)

  10. #8
    Mario2302's Avatar
    Joined
    Jun 2015
    Posts
    2,379
    Userbars
    63
    Thanks
    4,849
    Thanked
    4,671/1,964
    DL/UL
    44/0
    Mentioned
    406 times
    Time Online
    121d 18h 8m
    Avg. Time Online
    54m
    i'm an uninitiated person to be honest so i've just browsed the web a bit.

    Perhaps you can find your answer (you need an account to see links)as they seem to be solving a very similar issue

    ---

    Or just follow the post above as this person is clearly in the known
    he's also explaining the same thing as on the page i linked but in a better way.

  11. The Following User Says Thank You to Mario2302 For This Useful Post:

    linttastic (06-02-2020)

  12. #9

    Joined
    Apr 2020
    Posts
    24
    Thanks
    8
    Thanked
    12/8
    Mentioned
    1 time
    Time Online
    9h 24m
    Avg. Time Online
    N/A
    @(you need an account to see links)
    Thank you thank you thank you!!!! I had a suspicion the TRY and EXCEPT were involved with this, but the lectures and textbook for my course didn't explain it clear enough to apply to my assignment. Same with the 'None' affecting my smallest = num! I was able to get my code to run successfully, but more importantly, understand what I was doing wrong! Super helpful! Thank you again!!!


    @(you need an account to see links)
    Oh wow thanks for sharing that link! I hadn't thought that someone had posted elsewhere for this course assignment! Helpful to see how other people respond to that issue as well!
    Last edited by linttastic; 06-02-2020 at 08:35 AM.

  13. The Following 2 Users Say Thank You to linttastic For This Useful Post:

    aaronsd (06-02-2020),Mario2302 (06-02-2020)

  14. #10

    Joined
    May 2016
    Posts
    217
    Userbars
    9
    Thanks
    437
    Thanked
    229/96
    DL/UL
    17/0
    Mentioned
    18 times
    Time Online
    7d 14h 56m
    Avg. Time Online
    3m
    Quote Originally Posted by linttastic View Post
    @(you need an account to see links)
    Thank you thank you thank you!!!! I had a suspicion the TRY and EXCEPT were involved with this, but the lectures and textbook for my course didn't explain it clear enough to apply to my assignment. Same with the 'None' affecting my smallest = num! I was able to get my code to run successfully, but more importantly, understand what I was doing wrong! Super helpful! Thank you again!!!


    @(you need an account to see links)
    Oh wow thanks for sharing that link! I hadn't thought that someone had posted elsewhere for this course assignment! Helpful to see how other people respond to that issue as well!
    I'm very glad to hear that my detailed explanation helped your learning process makes it worthwhile to know the time I spent on that helped someone out haha. I'll be keeping an eye on this subforum to see if you end up posting any more programming brainteasers!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •