Home > Back-end >  removing the last comma and print the list inside square bracket
removing the last comma and print the list inside square bracket

Time:11-07

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many Fibonacci numbers would you like to generate? "))

# first two terms
n1, n2 = 1, 1
count = 0

# check if the number of terms is valid
if nterms == 0:
   print(f'The first 0 Fibonacci numbers are [].')
elif nterms == 1:
    print(f'The first 1 Fibonacci numbers are [1].')
elif nterms == 2:
    print(f'The first 2 Fibonacci numbers are [1, 1].')
# if there is only one term, return n1

# generate fibonacci sequence
else:
   print(f"The first {nterms} Fibonacci numbers are ", end = '')
   while count < nterms:
       print(*n1, sep=', ')
       nth = n1   n2
       # update values
       n1 = n2
       n2 = nth
       count  = 1

input: 10

expected output: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

my output: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,

CodePudding user response:

First, you're using the * operator wrong. It looks like you are a beginner, so I suggest not using it until much later (if you're up for it, you can do some reading on it here).

To fix your solution nicely and easily, all you need to do is put the values into a list and print the list because this is the default format for printing a list. Like this:

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many Fibonacci numbers would you like to generate? "))

# first two terms
n1, n2 = 1, 1
count = 0

# check if the number of terms is valid
if nterms == 0:
   print(f'The first 0 Fibonacci numbers are [].')
elif nterms == 1:
    print(f'The first 1 Fibonacci numbers are [1].')
elif nterms == 2:
    print(f'The first 2 Fibonacci numbers are [1, 1].')
# if there is only one term, return n1

# generate fibonacci sequence
else:
   print(f"The first {nterms} Fibonacci numbers are ", end = '')
   nums = []
   while count < nterms:
       nums.append(n1)
       nth = n1   n2
       # update values
       n1 = n2
       n2 = nth
       count  = 1
   print(nums)

Also, if you'd like, you can also simply your program to this:

nterms = int(input("How many Fibonacci numbers would you like to generate? "))

n1 = 1
n2 = 1

nums = []
for count in range(nterms):
    nums.append(n1)
    nth = n1   n2

    n1 = n2
    n2 = nth
print(f"The first {nterms} Fibonacci numbers are {nums}")

CodePudding user response:

You could just store your result in list and print it on the end. With such approach your program could be simplified!

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many Fibonacci numbers would you like to generate? "))

# first two terms
numbers = [1,1]
count = 2

# check if the number of terms is valid
if nterms <=2:
   print(f'The first {nterms} Fibonacci numbers are {numbers[:nterms]}.')
# generate fibonacci sequence
else:
   while count < nterms:
       nth = n1   n2
       # update values
       n1 = n2
       n2 = nth
       count  = 1
       numbers.append(nth)
   print(numbers)
  • Related