Hello everyone!
I am new to this forum and currently am enrolled in an intro to ECS class where we are working on python programming. This is our last assignment for python and I have been able to do the others successfully but am having some troubles with this one. I have read the different chapters in the book and have tried to google my problem but have not been able to find any solution to help me yet...hence why I am here!
Basically I need to create a program that will first as the user to input a positive integer. If the input is not a positive integer (i.e. negative, letter, etc.) they will get an error message asking them to input a positive integer. When they do input a positive integer the program will generate a random sequence of numbers of length determined by the user. For example if the user inputs the number 5, the program will spit out [1, 3, 6, 4, 9] and will then tell the user which number is largest in the sequence.
So far I this is what I have come up with:
However there are a few issues that I am encountering with my code. First I do not know where to implement the isdigit() function so that the user cannot input a letter. Right now when a letter is inputed I just get a ValueError message. Second...my program always comes up with the same integers in the same order. They are 1, 8, 7, 2, 4, 4, etc. Is there anyway that I can fix this? Should I try to implement a random.shuffle function and if so how would I do that?
Thank you very much in advance for any help you can provide . I have been stuck on this for about 5 hours now (I know pathetic) and as a final attempt decided to post on this forum! I am currently using Python 3.1.
I am new to this forum and currently am enrolled in an intro to ECS class where we are working on python programming. This is our last assignment for python and I have been able to do the others successfully but am having some troubles with this one. I have read the different chapters in the book and have tried to google my problem but have not been able to find any solution to help me yet...hence why I am here!
Basically I need to create a program that will first as the user to input a positive integer. If the input is not a positive integer (i.e. negative, letter, etc.) they will get an error message asking them to input a positive integer. When they do input a positive integer the program will generate a random sequence of numbers of length determined by the user. For example if the user inputs the number 5, the program will spit out [1, 3, 6, 4, 9] and will then tell the user which number is largest in the sequence.
So far I this is what I have come up with:
Code:
import random
random.seed(1)
integer = int(input("Please enter a positive number:"))
while integer <= 0:
integer = int(input("This is not a positive integer. Please enter a positive integer:"))
if integer > 0:
L = [random.randrange(10) for k in range (integer)]
largest = max (L)
print ("The random sequence is ",L,". The largest number in the sequence is ",largest,".",sep="")
input("\n\nPress the enter key to exit.")
However there are a few issues that I am encountering with my code. First I do not know where to implement the isdigit() function so that the user cannot input a letter. Right now when a letter is inputed I just get a ValueError message. Second...my program always comes up with the same integers in the same order. They are 1, 8, 7, 2, 4, 4, etc. Is there anyway that I can fix this? Should I try to implement a random.shuffle function and if so how would I do that?
Thank you very much in advance for any help you can provide . I have been stuck on this for about 5 hours now (I know pathetic) and as a final attempt decided to post on this forum! I am currently using Python 3.1.
Comment