I could use a bit of help with an assignment for class.

3    04 Feb 2018 05:05 by u/Firevine

This is going to be pathetically easy for you guys, but I am super, super new to this, and this textbook is a pile of hot garbage.

I need to debug this.

I get that there's currently no way to input test3, but I'm not sure how to add that simply. We can't rewrite the whole shebang, rather we have to work within the confines of how it's already written, making as few changes as possible. I'm thinking I just need to add the following two lines to the mainloop module

output "Enter score for test 3"

input test3

The (very) brief rundown on Modularization describes the benefits which I get, and the syntax, buuuuuuuut that's about it.

Thanks in advance.

10 comments

2

Step 1, I would enter a second line in housekeeping():

    input test1

Step 2, I would enter two lines in mainLoop() after "input test2":

    output "Enter score for test 3"
    input test3

Done.

0

Oh hey cool, I had it half right then. I did step 2 last night before seeing your response, but I did not do step 1. Thanks a ton! It's clicking a bit now (I was stressing out last night). I'm trying so hard to maintain this GPA that I think I'm psyching myself out on things.

0

Seems right to me?

0

The first problem I see is that it tests an undefined number for a value. test1 is declared, not given a value, and then a while loop is run based on its value.

0

If we were to unroll the logic, here is what you want the program to do:

output "Enter score for test 1 or negative number to quit"
input test1
if test1 < 0: goto end
output "Enter score for test 2 or negative number to quit"
input test2
if test2 < 0: goto end
output "Enter score for test 3 or negative number to quit"
input test3
if test3 < 0: goto end
average = (test1 + test2 + test3) / 3
output "Average is ", average
end:
output "End of program"

Obviously there is much repetition here, not to mention the goto's, which is why we want to structure it better. But this is what you want your program to achieve, and (usually mentally) laying it out in a linear fashion like this is very useful for debugging (make a habit of it).

  1. Is this what your program does? Go through line by line and look for discrepancies.
  2. Obviously the repeated blocks should be looped, the goto condition becoming the loop condition. Compare to your current loop condition.
  3. Note that the loop condition cannot be checked before the corresponding input, it has to be after.
  4. Note we have to skip the average calculation and output if we aborted.
  5. Have you covered arrays/lists? That would help here considerably.
0

Actually, scratch that... I should have paid more attention to the comment at the top. ("for any number of students ... until user enters negative value for first test score")

This is what they want it to do:


start:
output "Enter score for test 1 or negative number to quit"
input test1
if test1 < 0: goto end
output "Enter score for test 2"
input test2
output "Enter score for test 3"
input test3
average = (test1 + test2 + test3) / 3
output "Average is ", average
goto start
end:
output "End of program"

This is a bit different. Ignore 2, 4, 5 above. 1 and 3 still apply.

  1. The input of test1 is what they called "housekeeping". Except where is the input?
  2. End is still fine
  3. The big block is what they called mainloop (I wouldn't worry about the repetition, and I assume you haven't covered arrays yet)
  4. The tricky bit is the loop condition. You need "housekeeping" inside the loop, but you cannot check test1 until after it has run.

Have you covered break/continue? Or "do while" loops as opposed to while loops?

0

Have you covered break/continue? Or "do while" loops as opposed to while loops?

Not yet. We're only into the second chapter of the textbook. It's just...not a great textbook... I sent a shot of one of the pages to my friend and he was confused as to why they were even going on about some of the things they were.

I find it difficult to get into someone else's head quite often, so following the logic of why the writers wrote this the way they did is tough for me. It's kind of unintuitive. Why a negative value to exit rather than an exit button, or even an exit command if this were a command line program? Weird. That's not the scope of the assignment though, so I shouldn't dwell on it, lol.

I appreciate the assistance. Looks like I was on the right track, but I mainly wasn't sure if I had to write an additional module for test 3.

0

I find it difficult to get into someone else's head quite often, so following the logic of why the writers wrote this the way they did is tough for me

It's tough for everyone, as you can see by my initial mistake. It's often down to a keyword or two. That's the trouble with examples like this. They tend to be odd and intuitive, and have multiple solutions even though the authors want only a very specific one. It's just how it is.

Why a negative value to exit rather than an exit button...

If you're just starting, you probably won't be dealing with GUI applications any time soon.

... or even an exit command if this were a command line program?

How would you implement an exit command? Type in "exit" instead of a test score?

The test scores are declared as numbers. They won't accept "exit" as an input. Once you encounter an "input testx", the user must input a number to continue. -1 can never be a valid test score, so it's a convenient "get me out of here". You'll encounter -1 being used to indicate "not valid", "error", "exit" all over the place. It's a very common convention.

You could have an additional "Do you want to continue (yes/no):" input as part of the loop. But that would involve more code that has nothing to do with what this example is about and get annoying if you want to input 30 test scores.

0

If you're just starting, you probably won't be dealing with GUI applications any time soon.

Might be part of my confusion! I keep visualizing these as very basic Win98 looking GUI programs as I go through the steps. Also the last time I coded anything at all was farting around with HTML on an Angelfire site...

0

I like that. It really like I like kiểm tra số điện thoại viettel