Hunter White
Hunter White

Reputation: 41

What am I doing wrong in my College class program

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 enter image description here

Upvotes: 1

Views: 98

Answers (3)

helumpago
helumpago

Reputation: 106

From what I understand, your assignment is asking you to:

  1. Generate a bunch of random numbers.
  2. Remember those random numbers.
  3. Print out the numbers and find the highest and lowest numbers.

Right now, what your code is doing is:

  1. Generating a bunch of random numbers and printing out each individual number.
  2. Remembering only the last number.

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

MjH
MjH

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

Milan Tejani
Milan Tejani

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

Related Questions