PDA

View Full Version : Python HMWK Help?



linttastic
05-31-2020, 10:31 PM
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:

35187

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! :D:D

j03
05-31-2020, 10:57 PM
The error message which is in that black pop-up tells you what's wrong. Line 10:


p = computepay(h*r)

Should probably be:


p = computepay(h,r)

You're using an asterisk (*) instead of a comma (,). :P
linttastic

linttastic
05-31-2020, 11:51 PM
35189

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!!!

linttastic
06-01-2020, 10:04 PM
Hi all I'm back :P 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':

35224

Thank you in advance! :D

aaronsd
06-01-2020, 10:14 PM
Hi all I'm back :P 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':

35224

Thank you in advance! :D

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

linttastic
06-01-2020, 10:44 PM
---------- Post added at 10:44 PM ---------- Previous post was at 10:44 PM ----------


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 :S

aaronsd
06-02-2020, 12:16 AM
---------- 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 :S

Okay so there's two big problems here still.

1) Your TRY and EXCEPT are malformed. What you've done is this:


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


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


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:


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 :)

Mario2302
06-02-2020, 12:25 AM
i'm an uninitiated person to be honest so i've just browsed the web a bit.

Perhaps you can find your answer here ([Only registered and activated users can 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.

linttastic
06-02-2020, 08:33 AM
aaronsd
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!!! :D :D


Mario2302
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!

aaronsd
06-02-2020, 08:37 AM
aaronsd
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!!! :D :D


Mario2302
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!

linttastic
06-19-2020, 10:31 PM
I'm back with more Python hmwk ;_;

I feel like I have some of the right pieces, but it's scattered and in the wrong order. I know that some pieces are incorrect (lines 6-10) or duplicated because I copied over some code from a previous assignment I did that had some lines I needed, but with different outputs. I need help in organizing these functions in order to reflect the assignment instructions. I always seem to get pieces of it, but mess up with putting things together correctly. Thanks in advance to any input you guys give and also thanks for your patience with my slow comprehension of Python :hopelessness: <3

36007

linttastic
06-19-2020, 10:33 PM
Oh and the txt file we're pulling from has a bunch of "X-DSPAM-Confidence: 0.#####" lines that we're referring from along with other random text lines.

aaronsd
06-20-2020, 08:38 AM
Okay so a few pointers to give you some food for thought.

1) Sounds like you need to extract each of the floating point numbers referred to in ## in each line of the source file being read in that matches this format "X-DSPAM-Confidence: #.#####"

2) So, during the loop when it finds that match type, then the program should keep track of the total addition of those numbers found to date

3) After the loop that total number should be divided by the number of times those lines were found, which would give you the average across all the numbers found that match that format

4) Keep in mind how Python does blocks, is your FOR loop working correctly the way it is? Why is there a total=0 on line 11, isn't that going to reset the value of total after the loop? Don't you want to add the value to the "total" inside the loop? Also why is count=0 in line 12 that is just going to reset the count on the loop too.

Have a think of it and report back with what you tried out :)

linttastic
06-27-2020, 10:02 PM
Okay so a few pointers to give you some food for thought.

1) Sounds like you need to extract each of the floating point numbers referred to in ## in each line of the source file being read in that matches this format "X-DSPAM-Confidence: #.#####"

2) So, during the loop when it finds that match type, then the program should keep track of the total addition of those numbers found to date

3) After the loop that total number should be divided by the number of times those lines were found, which would give you the average across all the numbers found that match that format

4) Keep in mind how Python does blocks, is your FOR loop working correctly the way it is? Why is there a total=0 on line 11, isn't that going to reset the value of total after the loop? Don't you want to add the value to the "total" inside the loop? Also why is count=0 in line 12 that is just going to reset the count on the loop too.

Have a think of it and report back with what you tried out :)

Hey thanks for breaking it down for me! I got it set up properly after I looked over the FOR loop. Later, it turned out I had my float set up wrong that it ended up returning a nan error. Well all done now anyway, phew! Thanks again for helping! I appreciate having someone else just sort of re-read it for me so I can make sure I understand the assignment at least haha.

aaronsd
06-27-2020, 11:28 PM
Hey thanks for breaking it down for me! I got it set up properly after I looked over the FOR loop. Later, it turned out I had my float set up wrong that it ended up returning a nan error. Well all done now anyway, phew! Thanks again for helping! I appreciate having someone else just sort of re-read it for me so I can make sure I understand the assignment at least haha.Glad to know you sorted it out :D always happy to discuss and help!