0

I have a problem that requires me to take in a users input and return all items of that value or higher than that value up to 100

im asking the user what grades he wants to see from a set of data that i have.

So i will have the user input a grade and i will return all records of people with that grade or higher.

here is what i have so far

a small sample of the data that im pulling from looks like this

 ['Bud', 'Abbott', 51, 92.3]
 ['Don', 'Adams', 51, 90.4]
 ['Mary', 'Boyd', 52, 91.4]
 ['Jill', 'Carney', 53, 76.3]
 ['Hillary', 'Clinton', 50, 82.1]
 ['Randy', 'Newman', 50, 41.2]

My code so far is just a few if and elif statements making sure the user enters the correct function. This function is going to work so that if the user enters the letter g the program will ask for a grade threshold, and then will return any lines of data with that grade and above.

For example if i were the user, and i input g and then input 90 i would only get back these three lines

 ['Bud', 'Abbott', 51, 92.3]
 ['Don', 'Adams', 51, 90.4]
 ['Mary', 'Boyd', 52, 91.4]

Also if the user enters the letter S it finds the records of that section and returns with all the students in that section so if the user enters s and then 50 the program will return with

 ['Hillary', 'Clinton', 50, 82.1]
 ['Randy', 'Newman', 50, 41.2]

the code i have written so far looks like this

def Query ():
    input("enter query type (g or s):")
    #checks user's input and takes user to grades
    if (operator == "g"):
        print("You have chosen to query Grades")
    GradeThreshold=input("enter the Grade threshold:")


    #checks user's input and takes user to section 
    elif (operator == "s"):
         print("You have chosen to query Section")
    SectionNumber=input("enter the section:")


    elif (operator != "g") and (operator != "s"):
          print("Invalid entry. Please re-enter the operation from above.")
    return()

I am stumped on how i would take the users input and have it select the range of grades or the section numbers from the list of data i have above. Please help me.

spenman
  • 653
  • 3
  • 7
  • 11

1 Answers1

0

You need to iterate over the items. For example:

items = [['Bud', 'Abbott', 51, 92.3],
         ['Don', 'Adams', 51, 90.4],
         ['Mary', 'Boyd', 52, 91.4],
         ['Jill', 'Carney', 53, 76.3],
         ['Hillary', 'Clinton', 50, 82.1],
         ['Randy', 'Newman', 50, 41.2]]

for item in items:
    print(item)

This will print all items, in order. To get a value from the item, you need to either use brackets to access it by index:

for item in items:
    print item[2] # Prints the 3rd element in item (because indexes start at 0)

or unpack the items as you iterate:

for first_name, last_name, some_integer, grade in items:
    print('Name:', first_name, last_name)
    print('Grade:', grade)

This second solution is seen as more idiomatic when you have few items in each item, it's preferred because it's clearer what the items are composed of.

Assuming it's homework, I think you can finish from here.

BoppreH
  • 8,014
  • 4
  • 34
  • 71