Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 22nd, 2013, 09:47 PM
 lunapt
Registered User

Join Date: Jun 2013
Posts: 12
Time spent in forums: 3 h 35 m 26 sec
Reputation Power: 0

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
July 22nd, 2013, 10:05 PM
 Cameron0960
Contributing User

Join Date: Feb 2012
Posts: 103
Time spent in forums: 1 Day 28 m 24 sec
Reputation Power: 2
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.

#3
July 22nd, 2013, 10:11 PM
 lunapt
Registered User

Join Date: Jun 2013
Posts: 12
Time spent in forums: 3 h 35 m 26 sec
Reputation 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?

#4
July 22nd, 2013, 10:13 PM
 Cameron0960
Contributing User

Join Date: Feb 2012
Posts: 103
Time spent in forums: 1 Day 28 m 24 sec
Reputation Power: 2
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.

#5
July 22nd, 2013, 10:16 PM
 lunapt
Registered User

Join Date: Jun 2013
Posts: 12
Time spent in forums: 3 h 35 m 26 sec
Reputation Power: 0
doing that gives as many ouputs as the user's input #. i only want one list

#6
July 22nd, 2013, 10:18 PM
 Cameron0960
Contributing User

Join Date: Feb 2012
Posts: 103
Time spent in forums: 1 Day 28 m 24 sec
Reputation Power: 2
Quote:
 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.

#7
July 22nd, 2013, 10:18 PM
 lunapt
Registered User

Join Date: Jun 2013
Posts: 12
Time spent in forums: 3 h 35 m 26 sec
Reputation Power: 0
Enter a value for N : 3
list: [10,-1,8]
enter search value: 5
5 occurs in the list 0 times

#8
July 22nd, 2013, 10:20 PM
 Cameron0960
Contributing User

Join Date: Feb 2012
Posts: 103
Time spent in forums: 1 Day 28 m 24 sec
Reputation Power: 2
Quote:
 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.

#9
July 22nd, 2013, 10:22 PM
 lunapt
Registered User

Join Date: Jun 2013
Posts: 12
Time spent in forums: 3 h 35 m 26 sec
Reputation 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:"))```

#10
July 22nd, 2013, 10:23 PM
 Cameron0960
Contributing User

Join Date: Feb 2012
Posts: 103
Time spent in forums: 1 Day 28 m 24 sec
Reputation Power: 2
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.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Simple homework help please?

## Developer Shed Advertisers and Affiliates

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |