我必須編寫一個程序,它接受輸入並寫出輸入的數字以及它的等級。我的程序工作,但顯然我只能使用局部變量。我需要改變什麼,以便它只是局部變量?下面的兩個變量是我目前用作全局變量的。如何在不使用全局變量的情況下編寫此代碼?
# This program takes intger inputs and provides a grade value for each number entered
number = 1
myNumbers = []
print("Please enter as many integer numbers as you would like. Enter -999 to exit loop")
# This function has the user input values and exit with a sentinel value -999
def getScores(number):
sentinelValue = -999
while(number != sentinelValue):
number = eval(input("Enter an integer: "))
if number > -1:
myNumbers.append(number)
print("Numbers entered: ")
print(myNumbers, "\n")
# This function prints out the values with the appropriate letter grade
def printScores():
print("Length of array = ", len(myNumbers))
for i in range(0, len(myNumbers)):
if myNumbers[i] >= 90:
print(myNumbers[i], "is an A")
if myNumbers[i] >= 80 and myNumbers[i] < 90:
print(myNumbers[i], "is a B")
if myNumbers[i] >= 70 and myNumbers[i] < 80:
print(myNumbers[i], "is a C")
if myNumbers[i] >= 60 and myNumbers[i] < 70:
print(myNumbers[i], "is a D")
if myNumbers[i] >= 0 and myNumbers[i] < 60:
print(myNumbers[i], "is an F")
# The main function is where we will call the other functions and print out the results
def main():
getScores(number)
printScores()
# Call the main
main()
我仍然應該有兩個單獨的功能。
在此處發佈代碼以實現永久化。 – tdelaney