#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Simple homework help please?


    enerate a list of N (input by user) random integers where each integer is between -10 and 10. Output the list. Then ask the user to input a value (lets say X) to search for in the list. Finally output the number of times X occurs in the list. This part should be done separately (i.e. in a different loop) to the first part where you are generating the list.
    Example would be:
    Code:
    Enter a value for N : 3
    list: [10,-1,8]
    enter search value: 5
    5 occurs in the list 0 times

    I was thinking something like:

    Code:
    ValueN = int(raw_input("Enter a value for N:"))
    import random
    ListA= []
    for i in range [-10,10]:
        ListA.append(random.randrange(ValueN)
    print ListA
    which is wrong.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    It's great that you are starting by doing one feature at a time. That's a fantastic way to go about programming!

    So, judging by your code you are wanting to generate N random values between -10 and 10. Why are you looping from -10 to 10?

    Generally speaking all your import statements should go at the very top of your program. That's just a general programming standard that most abide by.

    randrange was the right choice. You aren't using it properly, though.

    The method signature looks like this: random.randrange(start, stop[, step])
    http://docs.python.org/2/library/random.html

    Take a look at the API and see if you can figure out why yours isn't working. Think carefully about the range between which you want to generate your random numbers from.

    Your also missing a bracket on the statement in your for loop.
    Last edited by Cameron0960; July 22nd, 2013 at 10:11 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    import random
    N = int(raw_input("Enter a value for N:"))
    ListA= []
    for each in range(N):
    random_num= random.randrange(-10,10)
    print random_num
    Search = int(raw_input("Enter a search value:"))


    The issue now is that the random_nums arn't in the list... what should i do?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Add them to the list. You were adding them to the list before. Take a look at your code in the first post to see how you were adding values to the list.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    doing that gives as many ouputs as the user's input #. i only want one list
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Originally Posted by lunapt
    doing that gives as many ouputs as the user's input #. i only want one list
    I am unsure of what you are wanting. Post your code with an example of proper input and output.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    Enter a value for N : 3
    list: [10,-1,8]
    enter search value: 5
    5 occurs in the list 0 times
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Originally Posted by lunapt
    Enter a value for N : 3
    list: [10,-1,8]
    enter search value: 5
    5 occurs in the list 0 times
    Thanks for posting that. I don't know what your current code looks like, though. Make sure to surround it with [code] tags otherwise its impossible to read.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    I just posted it ;s

    Code:
    mport random
    N = int(raw_input("Enter a value for N:"))
    ListA= []
    for each in range(N):
    random_num= random.randrange(-10,10)
    print random_num
    Search = int(raw_input("Enter a search value:"))
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Take a look at your code. There is an obvious problem that you can figure out on your own. Think about how Python distinguishes code blocks.

    Another hint: Take a look at the code in your first post for this topic. Compare it to your current solution.
    Last edited by Cameron0960; July 22nd, 2013 at 11:21 PM.

IMN logo majestic logo threadwatch logo seochat tools logo