Q. Write a program to increment the elements of a list with a number.


Answer =


lst = [ ]

while True :
      num = int(input("Enter a number :- "))
      lst.append(num)
      user = input("Do you want to quit enter yes =")
      if user == "yes":
            print(lst)
            break

Output :-

Enter a number :- 2
Do you want to quit enter yes =no
Enter a number :- 5
Do you want to quit enter yes =no
Enter a number :- 7
Do you want to quit enter yes =no
Enter a number :- 9
Do you want to quit enter yes =no
Enter a number :- 6
Do you want to quit enter yes =no
Enter a number :- 2
Do you want to quit enter yes =yes
[2, 5, 7, 9, 6, 2]

>>>

6 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. lst = [ ]

    while True :
    num = int(input("Enter a number :- "))
    lst.append(num)
    user = input("Do you want to quit enter yes =")
    if user == "yes":
    print(lst)
    break
    Output :-

    Enter a number :- 2
    Do you want to quit enter yes =no
    Enter a number :- 5
    Do you want to quit enter yes =no
    Enter a number :- 7
    Do you want to quit enter yes =no
    Enter a number :- 9
    Do you want to quit enter yes =no
    Enter a number :- 6
    Do you want to quit enter yes =no
    Enter a number :- 2
    Do you want to quit enter yes =yes
    [2, 5, 7, 9, 6, 2]

    >>>

    ReplyDelete
  2. l=[int(i) for i in input().split()]
    n=int(input())
    l1=l.copy()
    for i in range(len(l)):
    l[i]+=n
    print("original list=",l1)
    print("modified list=",l)

    THIS IS THE CORRECT CODE

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post