47.6k views
4 votes
Can someone help me out with this one? I'm not sure why my code is not working

Now, let's improve our steps() function to take one parameter
that represents the number of 'steps' to print. It should
then return a string that, when printed, shows output like
the following:
print(steps(3))
111
222
333
print(steps(6))
111
222
333
444
555
666
Specifically, it should start with 1, and show three of each
number from 1 to the inputted value, each on a separate
line. The first line should have no tabs in front, but each
subsequent line should have one more tab than the line
before it. You may assume that we will not call steps() with
a value greater than 9.
Hint: You'll want to use a loop, and you'll want to create
the string you're building before the loop starts, then add
to it with every iteration.
Write your function here
def steps(number):
i = 1
while i < number + 1:
string = str(i) * 3
string1 = str(string)
if i != 0:
string1 = (i * 4*' ' + "\b" + "\b" + "\b" + "\b") + string
elif i == 1:
string1 = string
print(string1)
i = i + 1
#The following two lines will test your code, but are not
#required for grading, so feel free to modify them.
print(steps(3))
print(steps(6)

User Arsalan T
by
4.4k points

1 Answer

5 votes

Answer:

Add this statement at the end of your steps() function

return ""

This statement will not print None at the end of steps.

Step-by-step explanation:

Here is the complete function with return statement added at the end:

def steps(number): # function steps that takes number (number of steps to print) as parameter and prints the steps specified by number

i = 1 #assigns 1 to i

while i < number + 1:

string = str(i) * 3 #multiplies value of i by 3 after converting i to string

string1 = str(string) # stores the step pattern to string1

if i != 0: # if the value of i is not equal to 0

string1 = (i * 4*' ' + "\b" + "\b" + "\b" + "\b") + string #set the spaces between steps

elif i == 1: # if value of i is equal to 1

string1 = string #set the string to string1

print(string1) # print string1

i = i + 1 # increments i at each iteration

return "" #this will not print None at the end of the steps and just add a an empty line instead of None.

Screenshot of the corrected program along with its output is attached.

Can someone help me out with this one? I'm not sure why my code is not working Now-example-1
User Daniel Bickler
by
5.0k points