Hi so i created a function that asks users to input their grades for a course and theoretically it should post all of the results and then the grade for the course. I have so far been able to create a function that asks the user for their information and then displays it:

``````def main ():

print()
a1 = getInputAsInt("What did you get on assignment 1?")
a2 = getInputAsInt("What did you get on assignment 2?")
a3 = getInputAsInt("What did you get on assignment 3?")
t1 = getInputAsInt("What did you get on test 1?")
t2 = getInputAsInt("What did you get on test 2?")
t3 = getInputAsInt("What did you get on test 3?")
ex = getInputAsInt("What did you get on the final exam?")

print()

print("The mark for assignment 1 is {numer}/{denom}".format\
(numer=a1, denom= 15))
print("The mark for assignment 2 is {numer}/{denom}".format\
(numer=a2, denom= 15))
print("The mark for assignment 3 is {numer}/{denom}".format\
(numer=a3, denom= 15))
print("The mark for test 1 is {numer}/{denom}".format\
(numer=t1, denom= 24))
print("The mark for test 2 is {numer}/{denom}".format\
(numer=t2, denom= 24))
print("The mark for test 3 is {numer}/{denom}".format\
(numer=t3, denom= 24))
print("The mark for the final exam is {numer}/{denom}".format\
(numer=ex, denom= 100))
print()

def getInputAsInt(questionForUser):
data = input(questionForUser)
data = int(data)
return data
``````

My problem is I don't know how to get python to use the information inputted in a weighted formula to calculate grade average. I know I have to call the function somewhere within the main code to get it to follow through.

I don't want someone to do the code for me, however I would like an explanation about working with user input.

Thank you so much

• Use the (CODE) button when posting code: It retains indentation, does code coloring and makes line numbers that we can use in our response. Either press it then paste your code between the tags, or paste your code, then highlight it all and press the (CODE) button.
• If you are …

## All 2 Replies

• Use the (CODE) button when posting code: It retains indentation, does code coloring and makes line numbers that we can use in our response. Either press it then paste your code between the tags, or paste your code, then highlight it all and press the (CODE) button.
• If you are using Python 2.x then you should use the `raw_input` function to get user input. If you are using Python 3.x, then you should use the `input` function. Confusingly, Python 2.x, also has a function named `input` that is not the right function to use.

(run this program to discover your python version):

``````import sys
print(sys.version_info)
``````

Your version is "major dot minor"

To use functions you need to think about the flow of the program: What happens first, then what happens, etc. In Python, we define functions in the top part of the file. Those definitions are seen before the program starts running. Once the program is running you send data to a function by passing it in as an argument; and it returns data which can be used however you need it. For instance

``````def getInt(prompt):
return int(raw_input(prompt))

def calculateAbsDifference(a,b):
diff = a - b
if b > a:
diff = b - a
return diff

def main():
print("We will calculate some absolute differences")
while True:
a = getInt("First number (99 to quit): ")
if 99 == a:
break
b = getInt("Second number: ")
print("The difference between %d and %d is %d"%(a,b,calculateAbsDifference(a,b)))
print("Thanks for calculating with me")

if __name__ == "__main__":
main()
``````
1. The program flow starts on line 20 where we see if the script is being run.
2. If so, we call `main()` on line 21
3. We enter main on line 10
4. on line 11 we print a message
5. line 12 is the top of a loop
6. line 13 asks for the first integer by calling `getInt()`
7. getInt returns an integer from line 2 back to line 13 in main where variable `a` gets the value
8. line 14 tests if variable `a` has value 99: If so we break the loop at line 15
9. line 16, like line 13 gets a value from `getInt()` and puts it in variable `b`
10. line 17 pretty-prints the results of the calculation which was done by function `calculateAbsDifference()` that takes `a` and `b` as arguments and returns an int.
11. the loop stops just below line 17, and we return to line 12
12. if we broke out of the loop, the program continues at line 18 with a polite message
13. main is finished so we return to line 21. There's nothing else to do, so the script stops

In your case, you will get values into variables, then pass those variables to a more interesting function which should either return results that are printed later, or print the results itself.

Nice one Gris. You are a teacher
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.