Page 4 of 4 First ... 234
  • Jump to page:
    #46
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    27
    Rep Power
    0
    Problem im working on:

    Write a python program that:

    - Takes a list of five numbers from the user
    - Prints the list
    - Prints the average
    - Modifies the list so each element is one greater than it was before
    - Prints the modified list

    My code:

    Code:
    list1 = []
    for i in range(5):
        user = int (input("Enter a number: "))
        list1.append(user)
    list2 = []
    for num in list1:
        num += 1
        list2.append(num)
        
    
    print("Your list of numbers is:", list1)
    print("The average is: ", (list1[0]+list1[1]+list1[2]+list1[3]+list1[4])/5)
    print("Your numbers +1 added to each: ", list2)
    This seems to work just fine, but, reading through the instructions, I get a feeling that I'm not suppose to create a 2nd list, but rather, just modify the first one. So my question is: how do I achieve the same result without resorting to a second list?

    Thanks in advance
  2. #47
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    You are going to need the indexes in order to change each item in place. Also your average should be more general so it could work for any list of numbers:

    python Code:
    mylist = [74,23,47,4,1]
    print(mylist)
     
    average = sum(mylist)/float(len(mylist))
    print(average)
     
    for i,value in enumerate(mylist):
        mylist[i] += 1
    print(mylist)

    -Mek
Page 4 of 4 First ... 234
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo