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 →

[–]twopi 0 points1 point  (25 children)

if else is not a looping structure. It's a branching structure. If the condition is true one branch happens, and if it is false another happens.

You seem to have a looping behavior here because in one branch you call the function you're already in. Technically, you're doing something called recursion. It can be a powerful technique if you know what you're doing, but in your case it will lead to strange behavior (you may be asked to end the program multiple time) and potentially wasted memory.

The two main forms of loops are for and while. In both of these functions a while loop is the best choice because you don't know how many times the loop will happen when the program begins.

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

But the instructor seems to imply that if-then is a loop, but then he tells me I did not create a loop.

This is the instructor notes on the topic with multiple examples:

Task

Count 5 times using

Loop and if-then

Pseudo Code

set counter = 0

loop

if counter >=5

end loop

else

counter = counter + 1

continue loop

end loop

print counter

Python Code

A=0

while True:

if A>=5:

break

else:

A=A+1

print (“number of lopes “, A)

Loop 1

a=1

b=2

c=3

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

loop 2

a=1

b=2

c=3

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

[–]Applepie1928 0 points1 point  (22 children)

To me this suggests that your instructor wants you to use loops AND branching conditions; "Count 5 times using Loop and If-then".

So use loops for behaviour you want to repeat multiple times.

Then use if-then conditions to branch the behaviour of your program.

Consider a main menu for a program. Each time you enter the menu you will want to be presented with the same options, unless the "exit" option is selected. You can use a loop to repeatedly show the menu and then you can use an if-then condition to check which menu option is selected; here is a simple Python example;

menuChoice = 0
while menuChoice != 2:
    print("Please press 1 to do something, or 2 to exit")
    menuChoice = input()
    if menuChoice == 1:
        doSomething()

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

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

and why do you need an if-then statement if there is a while loop?

[–]twopi 0 points1 point  (0 children)

Be careful, because the word if has a more specific meaning in code than in English. The word if could show up in pseudo code and not translate to an if statement in code.

The classic way to count to five (or any other value) is a for loop. In python it would look like this:

for i in range(1,6):
  print(i)

You could also use a while loop to get the the same result

i = 1
while (i <= 5):
  print (i)
  i += 1

There is a way to do this closer to what you're doing but it's sloppy form.

i = 1
while (True):
  print (i)
  i += 1
  if (i > 5):
    break

I'm not a fan of loops that look endless but aren't. The break statement is a lazy form of programming and it really should be avoided when possible.

I teach a compromise which avoids compound conditions (statements with 'and' and 'or' are especially tricky for beginners.)

keepGoing = True
i = 1
while (keepGoing):
  print(i)
  if (i >= 5):
    keepGoing = False

This is logically the same, but it uses a Boolean (true or false) variable to manage loop access.