Reputation: 41
So I am trying to get a program working for my class. I have to create a random number and then have it print off and from there I have to have it list the highest number and the lowest number. I am struggling to figure out how to get that to work.
import random
print("My Favorite Numbers")
for myFavNumbers in range(0,100,10):
myFavNumbers = random.randint(0,100)
print(myFavNumbers)
numSrch = (myFavNumbers)
for myFavNumbers in range(myFavNumbers):
if numSrch[myFavNumbers].upper()==numSrch.upper():
print(myFavNumbers[myFavNumbers])
it is a screenshot of the error
Upvotes: 1
Views: 98
Reputation: 106
From what I understand, your assignment is asking you to:
Right now, what your code is doing is:
Rather than saving the last number, you need to find a way record each number as it passes by. That is, you need to store the numbers in a list.
Once you've built a list of random numbers, you can use Python's built-in functions to do the rest of the work for you.
So, given all that, see if this code makes sense:
import random
print("My Favorite Numbers")
favoriteNumberList = []
for _ in range(0,100,10):
myFavNumber = random.randint(0,100)
favoriteNumberList.append(myFavNumber)
print(favoriteNumberList)
print(max(favoriteNumberList))
print(min(favoriteNumberList))
Upvotes: 0
Reputation: 1570
I hope I understood your task correctly. Is highest = max? Is lowest = min?
import random
random_numbers = []
print('My random numbers:')
for _ in range(10):
random_numbers.append(random.randint(0, 100))
print(random_numbers[-1])
print()
print(f'Max one: {max(random_numbers)}')
print(f'Min one: {min(random_numbers)}')
Upvotes: 2
Reputation: 372
Try this Code:
import random
print("My Favorite Numbers")
listofstud=[]
for myFavNumbers in range(0,100,10):
myFavNumbers = random.randint(0,100)
listofstud.append(myFavNumbers)
print("List of Student:"+str(listofstud))
print("Max Number:"+str(max(listofstud)))
print("Max Number:"+str(min(listofstud)))
this code will give max and min num from list
Upvotes: 1