String of Numbers
Write a function called string_of_numbers
that receives an integer length
and uses a while loop to create and return a string of consecutive numbers from 1 up to the number length
.
Examples:
>>> string_of_numbers(3)
'123'
>>> string_of_numbers(5)
'12345'
Hint:
You'll need variables to store your result string and a number keeping track of what loop your on.
You might need to use the str() function to change an integer into a string at some point.
Important: We've recorded a video with the solution of this exercise, in case you get stuck: https://youtu.be/i5Ve2ePni00.
Solution 1
def string_of_numbers(length):
number_string = ""
count = 1
while count <= length:
number_string += str(count)
count += 1
return number_string