Jump to content

[Python] New to coding and my teacher had a bad explanation

Natsoup

We had to make a calculator in class and I was struggling to make it loop. My teacher is pretty good at coding but he really didn't explain it well when he made a few changes and suddenly the loop worked. Can someone explain the while statement at the bottom of this code and the relationship between "repeat" and "main()"? And what does "return repeat" do? I'll leave out all the functions so that it's easier to read. thanks in advance

repeat = 1

def main():
    print("1: add")
    print("2: subtract")
    print("3: divide")
    print("4: multiply")
    print("5: area")
    print("6: diameter of a circle")
    print("7: circumference of a circle")
    choice=input("Input the number corresponding to the function you wish to use:")
    if(choice == "1"):
        add()
    if(choice == "2"):
        subtract()
    if(choice == "3"):
        divide()
    if(choice == "4"):
        multiply()
    if(choice == "5"):
        area()
    if(choice == "6"):
        diameter()
    if(choice == "7"):
        circumference()
    choice=input("Run again? Y/N:")
    if(choice=="y"):
        repeat = 1
    else:
        print("Goodbye")
        repeat = 0
    return repeat

while(repeat == 1):
    repeat=main()
    

 

qυoтe мe pleaѕe!

Me at the Apple store: "So how fast is this little macbook?"

Apple employee: "Cheetah Fast. Lightning Fast. It's Really, really, fast."

Link to comment
Share on other sites

Link to post
Share on other sites

The statement at the bottom insures that the main() method will loop if result = 1. The reason you defined main(), was to ensure that that snipit of code is recallable. Since it is recallable, every time the while loop well loops, it will run that same code again. In the main method, you are returning either 1 or 0 (Depending on what was typed) so that return from within the loop equals that number. If it is 1, then it runs once more.

Edit: @Natsoup Post was edited for more clarification

Edited by Guest
Clarification
Link to comment
Share on other sites

Link to post
Share on other sites

Return command jumps to a specific part of the code.

So return repeat makes the code jump back to the place with repeat name.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Disclaimer: I don't do Python except in specific circumstances so apologies if my terminology isn't exactly correct.

 

Paying attention to indentation for order of events is pretty key here. You initially set the variable repeat as 1. You then call the main() function which detects the type of calculation to do and then does that calculation function. Then you determine whether the user wants to continue with another calculation. Return is pretty common among different languages. It basically tells the system to output a value/variable from the function.

 

In this case, if the user says no to another calculation, main() outputs repeat with a value of 0 to the system and main() ends. Since you have not specified anything after that to execute in the case of repeat equaling 0, the program ends.

 

If the user says yes, main() outputs repeat with a value of 1. The difference here though is that the while loop at the end checks the value stored in repeat, specifically checking for a value of 1. Since the value this time is 1, the loop calls the main() function again to go through the calculation again and giving repeat the value from return repeat from the second time through main().

 

If during the second time the users says no to repeating, then that original repeat gets the value 0. The while loop checks to see the value of repeat, sees that the condition check comes back as false, does not iterate another time through the loop and ends the program.

 

If the users says yes a second time, the original repeat gets the value 1. The while loop sees that the condition check comes back as true and so it iterates through the loop once more, calling the main() again and giving repeat the returned value once more.

 

 

 

 

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

@DeadEyePsycho @tjcater

Thanks for your wonderful responses. I'm sorry if I'm a little slow - so

repeat=main()

sets repeat to the variable returned by the function main? And by that logic, if main hadn't returned repeat, it would break the program, correct?

Further, would repeat=add() not work unless add defined and returned repeat?

i'm trying to understand this completely, so I'm asking a lot of questions, thank you so much for your time :D 

qυoтe мe pleaѕe!

Me at the Apple store: "So how fast is this little macbook?"

Apple employee: "Cheetah Fast. Lightning Fast. It's Really, really, fast."

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Almostbauws said:

Return command jumps to a specific part of the code.

So return repeat makes the code jump back to the place with repeat name.

 

 

In this case would it jump to repeat=1 or repeat=main()?

qυoтe мe pleaѕe!

Me at the Apple store: "So how fast is this little macbook?"

Apple employee: "Cheetah Fast. Lightning Fast. It's Really, really, fast."

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Natsoup said:

so repeat=main() sets repeat to the variable returned by the function main?

Yes

Quote

And by that logic, if main hadn't returned repeat, it would break the program, correct?

Depends, but in this situation, no. If nothing is returned, repeat will just have a value of None. Keep in mind that in this case, repeat is not string, so using it as one could break your program. While evaluating Booleans (In the while loop, it checks to see if repeat = 1 is true.) or printing it by itself, this is a non-issue.

Spoiler

As you see in this snipit of code, it works fine.

image.thumb.png.c9ceb55573f0aa29661943cfe6baa17e.png

 

Quote

Further, would repeat=add() not work unless add defined and returned repeat?

It would work fine provided the situation is the same as the previous code. However if you don't plan to ever return a value, it is better to just use add() by itself instead of repeat = add().

Quote

i'm trying to understand this completely, so I'm asking a lot of questions, thank you so much for your time :D 

Np, its fun to help others get into programming.

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Natsoup said:

 

Thanks for your wonderful responses. I'm sorry if I'm a little slow - so

repeat=main()

sets repeat to the variable returned by the function main? And by that logic, if main hadn't returned repeat, it would break the program, correct?

Further, would repeat=add() not work unless add defined and returned repeat?

i'm trying to understand this completely, so I'm asking a lot of questions, thank you so much for your time :D 

The value being returned can be named anything.

 

17 minutes ago, Natsoup said:

In this case would it jump to repeat=1 or repeat=main()?

Return doesn't cause a jump, it simply ends the function you're currently in and sends a value as the result of the function.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, DeadEyePsycho said:

 

Return doesn't cause a jump, it simply ends the function you're currently in and sends a value as the result of the function.

Sorry for that misinformation. It turns out that in different languages the same word can mean a very different thing.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×