10/03/2020

Python Explanation

 Hello!

Today is the python explanation!

Today's question is this!

Create a function that takes a number (from 1 - 60) and returns a corresponding string of hyphens.

Examples

num_to_dashes(1) ➞ "-"

num_to_dashes(5) ➞ "-----"

num_to_dashes(3) ➞ "---"

From : Convert Number to String of Dashes // Edabit

This is my code.

My code.

def num_to_dashes(num):
output = ''
counter = 0
while counter < num:
output += '-'
counter += 1
return output

First, let's make a empty stringーoutput.

Next, let's make a counter setted to 0.

After that, let's make a While loop with a conditionーwhile the number is bigger than the counter.

Inside the while loop, we will plus the output with the Dash(-) and also plus the counter by 1.

After looping, we will return output.

What a easy code!


See ya!


0 件のコメント:

Super Leo 4!

  Leo : Let's do this! Reimu : I'll kill those monsters, so you go! Leo : Yeah! I'll go to the goal!  Leo : Let's go〜! ? Wai...