'Trying to make it so function main() recognizes specific integers when calling upon function problem_solving

I am a total novice so bear with me here. Basically, I want the main() function to be able to pick up on a specific integer. As in, if my input for step_graded was '1', I could write gradeforstep = problem_solving('1') and be taken to stepgrade=input('Please enter a score 0 - 5 earned for understanding step 1:'). Any help would be greatly appreciated.

def problem_solving(step_graded):
    if step_graded == '1':
        stepgrade=input('Please enter a score 0 - 5 earned for understanding step 1:')
        finalstepgrade1 = f"Step: {step_graded} Grade: {stepgrade}."
        return finalstepgrade1
    if step_graded == '2':
        stepgrade=input('Please enter a score 0 - 5 earned for understanding step 2:')
        finalstepgrade = f"Step: {step_graded} Grade: {stepgrade}."
        return finalstepgrade
    if step_graded == '3':
        stepgrade=input('Please enter a score 0 - 5 earned for understanding step 3:')
        finalstepgrade = f"Step: {step_graded} Grade: {stepgrade}."
        return finalstepgrade
    if step_graded == '4':
        stepgrade=input('Please enter a score 0 - 5 earned for understanding step 4:')
        finalstepgrade = f"Step: {step_graded} Grade: {stepgrade}."
        return finalstepgrade

def main():
    gradeforstep = problem_solving(input)
    print (gradeforstep)
main()


Solution 1:[1]

Here's an example of what might work. First, I simplified your problem_solving() function by using the step_graded argument explicitly; next, I replaced the main() function (which is likely a C-relic) with the pythonic way of doing things (i.e. the if __name__ == '__main__' clause); finally, I used sys.argv to read in the step_graded variable from the command line.

import sys

def problem_solving(step_graded):
    stepgrade = input(f'Please enter a score 0 - 5 earned for understanding step {step_graded}: ')
    finalstepgrade = f'Step: {step_graded} Grade: {stepgrade}'
    return finalstepgrade

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('argument missing for step_graded.')
        exit(1)

    step_graded = sys.argv[1]
    response = problem_solving(step_graded)
    print(response)

Thus, if you save the code as, say, mycode.py, then you run it as:

$> python mycode.py 3
Please enter a score 0 - 5 earned for understanding step 3: 2
Step: 3 Grade: 2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrej Prsa