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
2 Answers
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)
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
- Replace the while with "for tip in range(15, 30, 5):". Then you can get rid of initializing tip to 0, and needing an extra line of code in the loop to increment it.CommentedOct 8, 2022 at 3:59
- 1sure you're right... that's definitely a more optimal way. I was trying to demonstrate the concept of looping/iterating simply for this guy– boogCommentedOct 8, 2022 at 4:09
- Range is a bit confusing to look at if you're learning how to loop/iterate in python. But the incrementing a variable in a loop thing should almost never be done in python. It illustrates how loops work, but not how to use looping....CommentedOct 8, 2022 at 4:13