Python 3- assigns grades [duplicate]

2024/9/20 10:58:02

• Define a function to prompt the user to enter valid scores until they enter a sentinel value -999. Have this function both create and return a list of these scores. Do not store -999 in the list! • Have main( ) then pass this the list to a second function which traverses the list of scores printing them along with their appropriate grade.

I am having trouble with the getGrade function, it gives the error for i in grades: name 'grades' is not defined.

def main():grade = getScore()print(getGrade(grade))def getScore():grades = []score = int(input("Enter grades (-999 ends): "))while score != -999:grades.append(score)score = int(input("Enter grades (-999 ends): "))return gradesdef getGrade(score):best = 100for i in grades:if score >= best - 10:print(" is an A")elif score >=  best - 20:print(score, " is a B")elif score >= best - 30:print(score, " is a C")elif score >= best - 40:print(score, " is a D")else:print(score, "is a F")main()
Answer

You defined your function to be getScore() but you're calling getScores(), so as would be expected this throws an error because the function you're calling doesn't actually exist / hasn't been defined.

Addendum: Since you changed your question, after fixing the previous error.

Likewise you're calling grades, but grades is defined in your other function not within the scope of the function where you're trying to iterate over grades.

https://en.xdnf.cn/q/119773.html

Related Q&A

how to read video data from memory use pyqt5

i have an encrypted video file, i want to decrypt this file into memory and then use this data play video. but qt mediaplayer class is to pass a file name in, i need to have any good way?this is my co…

Pandas apply custom function to DF

I would like to create a brand new data frame by replacing values of a DF using a custom function. I keep getting the following error "ValueError: The truth value of a Series is ambiguous. Use a.e…

Economy Bot Daily Streak

I have a Discord.py economy bot that includes a daily command It gives everyone each day $50, but there is also a streak system. The first time they claim their daily, the bot gives them $50, day 2 is …

Normalise JSON with Python

Prompt me, please, how to normalize this JSON file using Python? This question is related to the previous The current JSON contains: {"total_stats": [{"domain": "domain.com&qu…

How to change decimal separator?

I have an Excel spreadsheet (an extract from SAP). I turn this into a DataFrame, do calculations and save it to an SQLite database. The Excel spreadsheet has comma as decimal separator. The SQLite data…

Unable to scrape the name from the inner page of each result using requests

Ive created a script in python making use of post http requests to get the search results from a webpage. To populate the results, it is necessary to click on the fields sequentially shown here. Now a …

Python Integer and String Using [duplicate]

This question already has an answer here:How can I concatenate str and int objects?(1 answer)Closed 7 years ago.for size in [1, 2, 3, 4]:result = 0print("size=" + str(size))for element in ra…

Beginner to python: Lists, Tuples, Dictionaries, Sets [duplicate]

This question already has an answer here:What is the difference between lists,tuples,sets and dictionaries? [closed](1 answer)Closed 3 years ago.I have been trying to understand what the difference is…

TypeError: NoneType object is not iterable in Python in csv

I am new to python, and trying to create a program which opens a csv file. The user is supposed to enter a barcode , then the program finds that product and the cost of the product. However I got an er…

No such Element Exception using selenium in python

from selenium import webdriver from selenium.webdriver.common.keys import Keys chrome_path=r"C:\Users\Priyanshu\Downloads\Compressed\chromedriver_win32\chromedriver.exe" driver=webdriver.Chro…