Home > other >  Python While Loop with Function
Python While Loop with Function

Time:10-08

My professor is asking for us to output the example on the left (word doc). My current code is on the right. He wants us to use a While/For loop in the code. I made the current code with only the 15% displayed (middle idle), but I do not know how to add the 20%,25%. Is this possible? Current code in screen clip

CodePudding user response:

To loop through a list of tip percent options I would suggest setting up something like this:

tip_percent_options = [0.15, 0.2, 0.25]

for tip_percent in tip_percent_options:
     
     # do the things you want to do with each tip option. Pass it to your tip_function etc.
     print(tip_percent)

Some examples of how to use loops here.

CodePudding user response:

honestly dude, just do your homework the right way. Even if you're not trying to become a dev you'll be so much better off in every STEM related field if you have these skills...

but anyways, like your prof. said you want to use a for or while loop to iterate through the tip amounts by a percentage of 5.

Here's a simple example to help you along where you appear to be stuck:

def main ():
    tip = 0
    iterator = 5
    while tip <= 25:
        print(tip)
        #  = is shorthand for 'tip = tip   iterator'
        tip  = iterator


if __name__ == "__main__":
    main()

output:

0
5
10
15
20
25

Implement something like this, or the 'for' loop example the other guy demonstrated into what you have and you should be set

  • Related