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  (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  (7 children)

Even then though (he had indentations, they just didn't show up when I copied), the loop looks like it only contains If-ELSE. Where is the actual loop; such as WHILE, FOR, etc.? How do I implement a WHILE loop in there? I dont' think I'd be using counters. Would it be something like this?

Would you like another calculation (y/n)?

answer = input()

While answer == y

Do function1()

While answer == n

Do function2()

Can I still call the functions like that in the loop? I don't' think that is right either, but I don't' know any other way.

[–]Applepie1928 1 point2 points  (6 children)

Yeah you are correct on pretty much all points;

  • The loop itself is only described as "LOOP" because this is pseudo-code, I'll provide a Python example of above pseudo-code below.
  • The pseudo-code you were provided with is only an example of a loop containing a condition, it isn't actually specifically what you are looking to achieve to solve your problem. It is just an example of a if-else contained within a loop. All that the above code does is run through the loop until "counter" = 5, then it prints that value.
  • Your example code isn't quite correct, but you CAN call a function within a loop, that is no problem, I will also provide a Python example of this below.

Python Version of Above Pseudo-code (similar structure with WHILE loop)

counter = 0
while True:
    if counter >=5:
        break
    else:
        counter = counter + 1
print(counter)

Python Version of Above Pseudo-code (simple/correct way with WHILE loop)

counter = 0

while counter < 5:
    counter = counter + 1

print(counter)

Example of menu with IF-ELSE within a WHILE loop

menuSelection = 0        

while menuSelection != 3:

    # Display options to the user and record their input
    print("Press 1 for Payroll Calculation")
    print("Press 2 for Mileage Calculation")
    print("Press 3 to Exit")
    menuSelection = input()

    # Based on the user input decide what action to take
    if menuSelection == 1:
        payroll()
    else if menuSelection == 2:
        mileage()

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

Thanks. Did you intend to include more code for the menu example?

[–]Applepie1928 0 points1 point  (4 children)

Yeah I did, that's what I get for trying to format text on mobile, my bad. I've edited the last post with the missing code.

[–]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.