In: Computer Science
In the previous question, you may notice that your line of numbers ends with the &. See if you can change this to only print the last number instead of the &. Hint: You’ll need to be clever about printing the last number from within the loop to make this work.
CODE:
#method 1 to declare a list
numbers = [4,5,6,7,8,9,0,4,6,1]
#method 2 to declare a list which contains 10 numbers each number
being 10
numbers = [10]*10
#method 3 to append 10 numbers in the list
numbers = []
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
#counting 1 to 10 using while loop
j = 0
while(j<10):
#loop runs 10 times
j += 1
#prints j on each loop
print(j)
print()
#counting 1 to 10 using for loop
for i in range(10):
#printing the numbers
print((i+1))
print()
#printing the numbers in the list
for j,i in enumerate(numbers):
#if the current number is not the last element in the list
#then it is printed with an & at the end
if(j!= len(numbers)-1):
print(i,end = "&")
else:
#otherwise just the number and then a new line
print(i)
____________________________________________
CODE IMAGES:
___________________________________________________
OUTPUT:
______________________________________________
Feel free to ask any questions in the comments section
Thank You!