Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
ok this is my last one and i am still a total n00b to this.
this is my assigment:
Lab: Finding the largest number

In this lab, you will write a program that finds the largest number of a sequence. First, your program asks the user to enter an integer number. Then the program creates a sequence of random integers that are less than 10 and outputs the largest number in the sequence. The number entered by the user determines the size of the sequence. For example, if the user enters 6, then the sequence should have 6 random integers.

Here’s an example of a run of the program. The output from the program is in blue, and the input (what you type) is in black.

please enter an integer: 0
This is not an integer greater than 0.
please enter an integer: a
This is not an integer greater than 0.
please enter an integer: 6

The random sequence is [1, 8, 7, 2, 4, 4]

The largest number in the sequence is 8

press enter to exit:

1. Coding the assignment
Open the python IDLE GUI. Similar to lab 6, you will be using raw_input function to get the user input, and printing out the final “Press enter to exist:” message. There is more than one way to do this lab, so we will not give step-by-step instructions. Some hints and requirements are:

· Your program should first import the random module. Right after that, you must put in this line of code (in red):
random.seed(1)
· You must check the input is actually an integer greater than 0. It cannot be 0 or any character. You can do this by using a while loop to keep asking the user until he/she enters the correct input.
· Once you get the input, you may use the randrange() function in the random module to generate an integer. How do you use randrange()? You do the following:
random.randrange(10)
This will randomly generate an integer number less than 10.
· To fill your sequence with the amount of integer specified by the user, you may use a for loop. Inside the for loop, you should call randrange to generate a random number and add that to your sequence.
· To find the largest number, you may use a for loop to go through each number in your sequence and determine which one is the largest.


i have something like this so far:
import random
random.seed(1)
In = raw_input("Please Enter An Integer:")


while In == 0 and string:
print "Please Enter An Integer Greater Than 0"

else:


random(In) = lister

for i in lister:
sort.list()
print lister

someone want to kick me in the right direction? i pretty much have to teach myself this because the professor doesn't really explain any of this.
 

rscott

macrumors member
Feb 17, 2008
43
0
The bottom of the assignment tells you pretty much exactly what to do, it just doesn't code it for you. What are you having trouble with specifically?
 

jtalerico

macrumors 6502
Nov 23, 2005
358
0
So the user is going to enter in the amount of random numbers they would like generated? So I would do that with the following:

Code:
times = raw_input("Number of random numbers to generate > ");
num_times = int(times); // This method way is assuming the user entered in something that is numeric, you could do a try catch block to ensure this.
values = [];
while ( num_times > 0 ) {
  values.append(random.randrange(10))
}

//Print results....

I have not tested this code... I didn't read your assignment either, but i hope this gives you a idea.
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
ok i got the code i am only missing one thing..

how do i make sure that when the user inputs a character that i can print "please enter a integer"
 

Nekrotized.butt

macrumors newbie
Mar 14, 2008
1
0
Whoh I'm in ECS 15 too
I have most of the code, i just can't get the whole random.range thing to work, what did you do? Can't figure out the character thing either
 

iHerzeleid

macrumors 6502a
Original poster
May 5, 2007
555
0
Whoh I'm in ECS 15 too
I have most of the code, i just can't get the whole random.range thing to work, what did you do? Can't figure out the character thing either

you have to make an empty list so something like this

new_list = [] #this makes you a new empty list which you will add on elements to

so to use the whole random range fx you have to run it thru a for loop.

so somthing like this...

for i in range(0, x*):
y*.append(random.randrange(10))


x* = name of your input (please enter a integer)
y* = name of your list


then just do a sort functions on that list (hint y*.sort()) as that uses the default parameters to list it in numerical order from smallest to largest..

then just pick the last element of that list and print that out.

hope i helped.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.