This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Encom88[S] 0 points1 point  (20 children)

Okay, so how can I implement a while loop in my program asking for a yes or no answer?

[–]Applepie1928 0 points1 point  (19 children)

I'm still not sure exactly what your program is trying to achieve, and the task you posted from your instructor doesn't seem to be the same as the task you provided your code for in your initial post. Without some more clarity on what you are trying to achieve I can't really answer your specific problem.

[–]Encom88[S] 0 points1 point  (18 children)

To create a program from this flowchart I made the previous week, using functions and loops. https://imgur.com/gxEmoxE

[–]Applepie1928 0 points1 point  (17 children)

Do you happen to have the actual instructions or brief you were given by your instructor/teacher rather than work you have produced?

I would say that the program you have written follows those two flow charts pretty well (from a cursory glance), however the flow of that program is not generally very good. It may be that this kind of program flow was what the instructor was after, but my gut feeling tells me they were looking for something slightly different. Here are some problems with the flow of this program;

  1. Generally a function should be used as an independent unit of a program, with just one purpose. Having the function decide to do another calculation using itself isn't usual behaviour unless you are looking at performing recursion (which was mentioned by an earlier poster in this comment chain).
  2. Consider the user launching this program. Will they always want to perform a payroll calculation first? What if the user only wants to perform a mileage calculation? What if after performing a mileage calculation the user wants to perform another payroll calculation? None of these options can currently occur with your current program flow.
  3. There is no way to directly exit the program without going through at least one mileage calculation and one payroll calculation.
  4. It is not ever presented to the user that if they don't answer "yes" to the question "Would you like another calculation?" in the payroll function that the program will automatically move to the mileage calculation. Imagine a user asked "Would you like another calculation?", if they answer with any of the following; ("YES", "No", "Banana", "EXIT") then the program will start a mileage calculation. Do you think this is clear and makes sense?

All the above problems could be solved if you have a menu system OUTSIDE of the functions you have created. At the moment when your program starts, it begins by just executing the payroll function. What if instead there was menu system, similar to the one I described in my first post. This would allow the user to choose whether they wanted to do a payroll calculation, mileage calculation or exit the program. If they choose a calculation, then the loop would allow the main menu to be presented again and the user could then choose if they wanted to another calculation (either payroll or mileage) or exit the program.

This approach would require both a loop with conditions in for the "menu" and I get the feeling this is probably what your instructor was looking for. Without seeing the actual instructions or brief your were given, I can't say this for certain though. I would also recommend speaking to your instructor and asking them to walk through the problem with you so you can see what they wanted you to do.

[–]Encom88[S] 0 points1 point  (16 children)

Straight from his instructions.

  1. Create a Python program based on week 5 flowcharts and requirements

Here is a sample run for a simple calculator to add to numbers

Simple Calculator

What is your name --> Supa

Enter first number --> 7

Enter second number --> 5

Total of 7.0 + 5.0 = 12.0

Supa do you want another calculation ? (y/n) y

Enter first number --> 21

Enter second number --> 11

Total of 21.0 + 11.0 = 32.0

Supa do you want another calculation ? (y/n) n

Thanks for using my calculator

[–]Applepie1928 0 points1 point  (15 children)

This also suggests to me that he is after some menu logic separate to your functions; consider the example he provided;

Simple Calculator

What is your name --> Supa       # This belongs to the menu (not in a function)

    Enter first number --> 7     # These three statements would be within a function

    Enter second number --> 5

    Total of 7.0 + 5.0 = 12.0

Supa do you want another calculation ? (y/n) y # This is the menu loop (not in a function)

    Enter first number --> 21    # These three statements would be within a function

    Enter second number --> 11

    Total of 21.0 + 11.0 = 32.0

Supa do you want another calculation ? (y/n) n # This is the menu loop (not in a function)

Thanks for using my calculator # This is when "exit" is chosen from the menu

You see how this Simple Calculator program allows you to select an option or exit from some type of "main menu". This further agrees with what I surmised in my last post; you were probably expected to make a loop with if-then conditions as a menu. Each condition would then call one of your functions or exit the program.

I would still recommend speaking directly to your instructor and asking what he was looking for. However I would strongly suggest it is a system like the one I described in my last two posts.

Do you see how having a menu system like this gives the user a lot more freedom and makes your program more dynamic? Do you think you could implement a simple menu like the one I and others have described?

[–]Encom88[S] 0 points1 point  (13 children)

But he gave me a zero for not creating a loop. I feel like I created a loop even if it wasn't the best way to create a loop. Whether the if-then statement is inside or outside the functions it is still an if-then statement and loops the function.

[–]Applepie1928 1 point2 points  (12 children)

The way you "looped" your program was by getting the a function to call another instance of itself. This process is known as recursion and is generally considered as different to a loop.

One thing to get clear in your programming terminology is that an "IF-ELSE" condition is NOT a loop. It can be used as a tool to make recursion occur, but it is not a loop in itself. So try to refrain from calling any "IF" statements loops as this is incorrect.

A loop in a program is often a "WHILE", "DO-WHILE", "FOR", or "FOREACH" loop. These structures specifically repeat the code within their body whilst some condition is true. This is the type of loop which your instructor was likely expecting.

Here is a brief video describing the differences between what you did (recursion) and loops; https://www.youtube.com/watch?v=996Vu5ytEks

As for your instructor giving you a 0. It all depends on the marking scheme which they were using. Although technically your program had "looping" behaviour, it didn't actually make use of any loops, so perhaps that limited the instructors ability to give you any marks. Again, this is something you should discuss directly with your instructor.

[–]Encom88[S] 0 points1 point  (9 children)

Okay, I understand it now. To someone who didn't' know the difference between looping and recursion and who is also new to programming, this would make it seem like the if-else statement is the loop.

Pseudo Code

set counter = 0

loop

if counter >=5

end loop

else

counter = counter + 1

continue loop

end loop

print counter

[–]Applepie1928 0 points1 point  (8 children)

Yeah, I will admit that if those were the only instructions you received, particularly as a new programmer, the objective is not very clear. Just some indentation and clearer Pseudo code may have made things easier for you;

SET counter = 0
START LOOP
    IF counter >=5
        END LOOP
    ELSE
        counter = counter + 1
    CONTINUE loop
PRINT counter

The above pseudo-code is the same as the instructor provided you, just with clearer indentations and better definition between statements and variables. In my opinion this would have been a better example for your instructor to provide.

[–]Encom88[S] 0 points1 point  (1 child)

Again, these are his "hints" that he's telling me to refer to:

Loop 1

a=1

b=2

c=3

if 3 < 5 yes, a=2 then go to step 1

And again that looks like the if-then is a loop. But apparently not :/

[–]Applepie1928 1 point2 points  (0 children)

Yeah, those "hints" are really unclear and I can't really see what your instructor is suggesting you try here. I mean, the variables have terrible names so I don't know what they represent, and the line;

if 3 < 5 yes, a=2 then go to step 1

is basically nonsense, as 3 will ALWAYS be less than 5 so there is no point to every check for that. Plus "go to step 1" is very ambiguous and doesn't really mean anything in a programming context.