grade.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. import argparse
  3. import sys
  4. wrong = 0
  5. questions = 0
  6. def parse_arguments():
  7. parser = argparse.ArgumentParser(description='pass.py')
  8. parser.add_argument('-q', '--questions', type=int, metavar='NUMBER', help='Number of questions')
  9. parser.add_argument('-w', '--wrong', type=int, metavar='NUMBER', help='Number of missed questions')
  10. parser.add_argument('-d', '--decimal', action="store_true", default=False, help='Output decimal')
  11. parser.add_argument('-c', '--chart', action="store_true", default=False, help='Output chart')
  12. args = parser.parse_args()
  13. return args
  14. def calculate(wrong, questions, args):
  15. correct = questions - wrong
  16. result = round((correct / questions) * 100, 2)
  17. if not args.decimal:
  18. result = round(result)
  19. return result
  20. def main():
  21. args = parse_arguments()
  22. if args.questions:
  23. print(f"{args.questions - args.wrong}/{args.questions} = {calculate(args.wrong, args.questions, args)}%")
  24. if args.chart:
  25. print("Wrong:\tGrade:")
  26. for i in list(range(1, args.questions+1)):
  27. #print(f"{i}\t{round(((questions - i) / questions) * 100)}")
  28. print(f"{i}\t{calculate(i, args.questions, args)}")
  29. else:
  30. sys.exit("Enter -q/-questions for the amount of questions. Please read the -h/--help.")
  31. if __name__ == "__main__":
  32. main()