Python Forum
Question about list and while function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about list and while function
#1
I am trying to code a list of 10 items whose values ​​come from the equation y=a*(b-x) and complete the rest of it with 0. e.g:
a=1
b=5
list=[]
x=0
while (x<=b):
      x=x+1
      y=a*(b-x)
      break
list.append(y)
list=[1,2,3,4,5,6,0,0,0,0,0]
Reply
#2
With your current code, you only need to remove "break" and fix the indenting to make it work. Then, we can use list.extend() to add the zeroes. On a related note, do not use reserved or implemented terms such as "list" as variable names. That has unintended consequences.

a=1
b=5
list_=[]
x=0
while (x<=b):
    x=x+1
    y=a*(b-x)
    list_.append(y)
list_.extend([0] * b)


That said, we can do better. As a general practice, avoid while loops. They have a tendency to become infinite loops when there's a bug. Instead, use for loops. A for loop is guaranteed to stop because it has a limited iteration. We can also implement x without a counter, cutting two lines out of the code, by implementing a for loop.

a=1
b=5
list_=[]
for x in range(1, 5):
    y=a*(b-x)
    list_.append(y)
list_.extend([0] * b)
But we can do even better! This is a more advanced technique. We have list comprehensions at our disposal. They're more efficient and sleek.

a=1
b=5
list_=[a * b - x for x in range(1,5)]
list_.extend([0] * b)
Reply
#3
I understand but the problem is that list must always have 10 elements and "a" and "b" will be input by the user.
Reply
#4
(Oct-11-2019, 03:54 AM)doug2019 Wrote: the problem is that list must always have 10 elements and "a" and "b" will be input by the user

Will this meet your needs?

a = 1
b = 5
mlist= [0]*(a - 1) + [a * (b - x) for x in range(a, b+1)] + [0]*(10 - b)
print(mlist)
Output:
[4, 3, 2, 1, 0, 0, 0, 0, 0, 0]

If you need to have the calculated values in ascending order, you could try the following:

a = 1
b = 5
mlist= [0]*(a - 1) + [a * (b - x) for x in range(b, a-1, -1)] + [0]*(10 - b)
print(mlist)
Output:
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0]
Reply
#5
If values of a & b are not restricted to within 10 (while their difference is so), and padding of zero values is only to be on the right side, you could try the following:

a = 12
b = 18
mlist= [a * (b - x) for x in range(b, a-1, -1)] + [0]*(10 - (b - a + 1))
print(mlist)
Output:
[0, 12, 24, 36, 48, 60, 72, 0, 0, 0]
Reply
#6
For the more general answer, how about
a = int(input("Enter a: "))
b = int(input("Enter b: "))

answer = [a * b - x for x in range(1,b+1)]
answer.extend([0] * (10-b))

print(answer)
Output:
Enter a: 3 Enter b: 6 [17, 16, 15, 14, 13, 12, 0, 0, 0, 0]
That works for any positive value of b < 10
Reply
#7
Thank you so much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question in python function problem saratha 1 252 Jul-08-2020, 04:56 PM
Last Post: jefsummers
  question about python3 print function jamie_01 5 599 May-25-2020, 09:58 AM
Last Post: pyzyx3qwerty
  Question about the groupby function new_to_python 7 609 Feb-09-2020, 07:52 AM
Last Post: perfringo
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 643 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Question on Join() function sduvvuri 2 593 Jun-02-2019, 03:55 PM
Last Post: perfringo
  question about list and remove function wmw12344 5 1,374 Mar-30-2018, 11:52 AM
Last Post: ljmetzger
  Basic input() function question rebubula76 2 1,307 Nov-27-2017, 08:12 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020