A repository for the git good Redbrick workshop.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

  1. #################################
  2. # CALCULATOR FUNCTIONS
  3. ################################
  4. def add(x, y):
  5. return x + y
  6. def sub(x, y):
  7. return x -y
  8. def mul(x, y):
  9. return x * y
  10. def div(x, y):
  11. return x / y
  12. def mod(x, y):
  13. z = x % y
  14. return z
  15. def exp(x, y):
  16. z = x ** y
  17. return z
  18. #################################
  19. # RUN CALCULATOR & MAIN FUNCTIONS
  20. #################################
  21. # DONT CHANGE THIS CODE
  22. def run_calculator(line):
  23. # set command as first word passed to the program
  24. command = line[0]
  25. # run until command is exit
  26. while command != "exit":
  27. # check that line is of format - `command x y`
  28. if len(line) > 3 or len(line) == 1:
  29. # tell user error occured and end program
  30. print("Error in number of arguments supplied!")
  31. break
  32. else:
  33. # set values of x and y
  34. x, y = int(line[1]), int(line[2])
  35. # get name of command passed by the user as a function
  36. func = globals()[command]
  37. # run command with x and y as parameters and print result
  38. print(func(x, y))
  39. # read in next lien of input and split on whitespace
  40. line = input().split(" ")
  41. # set new command
  42. command = line[0]
  43. def main():
  44. # split input on whitespace
  45. line = input().split(" ")
  46. # run calculator with split line
  47. run_calculator(line)
  48. if __name__ == "__main__":
  49. main()