Function Python Beginner

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

Hey,guys!Please,help me if I want to use a function three times ,how can I execute range three?For,example my function is -def frost_cupcakes() and I want to accomplish it three times,I do not want to copy-paste it thrice,how to call it three times then

Hey Marryrr,
Whenever you want to execute an action an x amount of times, the simplest way is to use a for loop with range. So for your function, you would have–

for x in range(3):
    frost_cupcakes()

Keep in mind range is exclusive, so range 3 means counting “0, 1, 2.” You can also specify a start and end point (as well as the step which is the 3rd optional parameter).

for x in range(0,3):
    frost_cupcakes()

will perform the same as the code above.

2 Likes

ok,thank you very much!