简体   繁体   中英

Python While Loop with Function

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM