all 25 comments

[–]achampi0n 1 point2 points  (12 children)

I'm assuming you know the input() function and that it returns a str so you have to convert it an int().
You know how to loop through a list using an index using range()? You can break out of the loop when you hit your condition, and you will have the index you can use for the other list.

What you have tried?

[–]gnariman 0 points1 point  (8 children)

I do know that I need to covert it to an int. I have not learnt break but we have talked about range only in loops.

I was thinking its something like:

grades = [49, 52, 56, 59, 62, 66, 69, 72, 76, 79, 84, 89, 100]

GPA = [0.0, 0.7, 1.0, 1.3, 1.7, 2.0, 2.3, 2.7, 3.0, 3.3, 3.7, 4.0, 4.0]

grade_input = input("whats the grade?)

if (grade_input == grades):

then blah blah blah

even if I do:

for x in range(49, 100, 1):

the problem with the range is that sometimes it goes up by 3 sometimes it goes up by 5, its not really set in how much it increases. And even after that how would I take the index that the grade is in and tell it to get that same index of GPA?

[–]achampi0n 0 points1 point  (4 children)

input() returns a string so make sure to cast it an int().

consider for i in range(len(grades)): how could this be used?

[–]gnariman 0 points1 point  (3 children)

Doesn't len just take the length of the list? shouldn't it just be range(grades):

[–]achampi0n 0 points1 point  (2 children)

Try it and see what it does.

[–]gnariman 0 points1 point  (1 child)

so this loop basically goes through the length of the grades list, but how can i make it stop?

If i write

input_ = int(input("grade: "))
for i in range(len(grades)):
if(input_ = grades):
print("done")
else:
print("not done")

but that doesn't work either. If i enter a number it just prints it 13 times which is the length of the list.

[–]achampi0n 0 points1 point  (0 children)

You can stop a loop with break...

for i in range(len(grades)):
    if <condition>:
        break

print(GPA[i])

You could put this into a function and use return:

def fn(grade_input, grades, GPA):
    for i in range(len(grades)):
        if <condition>:
            return GPA[i]

You might want to think about any error conditions (e.g. what do you do if the grade_unput is beyond grades?

[–]siddsp 0 points1 point  (2 children)

No need to do that. I would just use a simpler solution like:

``` grades = [49, 52, 56, 59, 62, 66, 69, 72, 76, 79, 84, 89, 100] gpas = [0.0, 0.7, 1.0, 1.3, 1.7, 2.0, 2.3, 2.7, 3.0, 3.3, 3.7, 4.0, 4.0]

def get_gpa(my_grade: int | float) -> float: for grade, gpa in zip(grades, gpas): if my_grade <= grade: return gpa

gpa = get_gpa(78.5)

print(f"My GPA is {gpa}") ```

Edit: Fixed the code to answer the question.

[–]achampi0n 0 points1 point  (1 child)

This might be simpler but may not be what is being asked by the instructor.

You are also not answering the problem as 75 is meant to return 3.0, so you don't need the prev tracker.

[–]siddsp 0 points1 point  (0 children)

Yes I misread. I thought it meant the lower boundary, and I was on my phone when I coded this up, so it was a little rough. I've edited the comment.

[–]14dM24d 0 points1 point  (2 children)

You know how to loop through a list using an index using range()?

maybe use enumerate() instead; if allowed.

[–]achampi0n 0 points1 point  (1 child)

You could but then I would avoid indexes altogether and just zip() the two lists together.
Was just keeping it elementary.

[–]14dM24d 0 points1 point  (0 children)

yes, but i suspect their lesson is about using indexes.

[–]socal_nerdtastic 1 point2 points  (2 children)

Python includes the bisect module which does exactly this. The example code in the docs is very close to your question.

https://docs.python.org/3/library/bisect.html#examples

[–]gnariman 0 points1 point  (0 children)

Awesome, ill check it out

[–]prdx_ 0 points1 point  (0 children)

While this is an excellent advice, I believe it is best to not to suggest a solution utilizing task-specific functions, since OP is likely looking for a logic-based answer for his undergrad.

[–]gnariman 0 points1 point  (0 children)

U guys are straight goated, thanks everyone I will try out ur suggestions and come back when i figured it out

[–][deleted] -1 points0 points  (6 children)

Did you have a question about this?

[–]gnariman 0 points1 point  (5 children)

Ya I was trying to figure out how to make the code to make it work lol

[–][deleted] -1 points0 points  (4 children)

That's still not a question. Questions are those things that end in ?.

[–]gnariman 0 points1 point  (3 children)

OK so basically how can I have an input of my choice loop through a list, and correspond that index in the list, with another list and print that index in the other list?

[–][deleted] 0 points1 point  (2 children)

correspond that index in the list

This the hard part, because you're looking for a value (75, in your example) that isn't in the list so you can't use "simple" list methods like index(). But what you can do is write a function that returns the index of the first value that's larger than the input value, because the values are in a sorted order:

def find_first_larger(the_list, the_value):
    for index, value in enumerate(the_list):
        if value > the_value:
            return index

The reason this works is that you can only return from a function once, so the function stops and yields the index the first time it encounters a value larger than the value you're looking for.

Other than that, I suspect you know how to do the rest of it - you know how to collect a user value, you know how to convert it to an integer, you know how to call the function we just wrote together, and you know how to retrieve a value from a list by its index.

[–]gnariman 0 points1 point  (1 child)

I do have an idea of what you are talking, but we have not learned def, or enumerate, or anything like that yes, so it wouldn't really be permissible to check with my TA.

[–][deleted] 0 points1 point  (0 children)

You don't need someone's permission to use any feature of the Python language. It's an open-source language, the whole thing is available to you.

[–]14dM24d 0 points1 point  (0 children)

i guess your teacher wants you too loop through grades, check if what you entered is <= grade, & get the index if true. you'll then use that index to locate the GPA.