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.

109 lines
2.4 KiB

  1. # converts string input (even fractions) to float
  2. def string_frac(in_string):
  3. if "/" in in_string:
  4. nd = in_string.split("/")
  5. n = float(nd[0])
  6. d = float(nd[1])
  7. ans = n/d
  8. return ans
  9. else:
  10. ans = float(in_string)
  11. return ans
  12. # Simplest one-step addition
  13. def one_step_add():
  14. import random
  15. # Display problem
  16. a = random.randint(-4,10)
  17. b = random.randint(2,24)
  18. print("x + ", a, " = ", b)
  19. ans = float(input("x = "))
  20. answer = b-a
  21. # Test input
  22. if ans==answer:
  23. print("Correct! \n")
  24. else:
  25. print("Try again")
  26. print("The correct answer is ", answer, "\n")
  27. # One-step additon with negative numbers
  28. def one_step_subtract():
  29. import random
  30. a = random.randint(-19,-1)
  31. b = random.randint(2,24)
  32. print(a, " + x = ", b)
  33. ans = float(input("x = "))
  34. # test
  35. answer = b-a
  36. if ans==answer:
  37. print("Correct! \n")
  38. else:
  39. print("Try again")
  40. print("The correct answer is ", answer, "\n")
  41. # One-step multiply
  42. def one_step_mult():
  43. # Uses string_frac(<input string>)
  44. import random
  45. a = random.randint(1,11)
  46. b = random.randint(2,24)
  47. print(a, "x = ", b)
  48. print("Round your answer to two decimal places.")
  49. ans_in = (input("x = "))
  50. answer = round(b/a,2)
  51. # test
  52. if string_frac(ans_in)==answer:
  53. print("Correct! \n")
  54. else:
  55. print("Try again")
  56. print("The correct answer is ", answer, "\n")
  57. # One-step divide
  58. def one_step_div():
  59. import random
  60. a = random.randint(1,11)
  61. b = random.randint(2,24)
  62. print("x/", a, " = ", b)
  63. ans = float(input("x = "))
  64. answer = b*a
  65. # test
  66. if ans==answer:
  67. print("Correct! \n")
  68. else:
  69. print("Try again")
  70. print("The correct answer is ", answer, "\n")
  71. # Two-step problems
  72. def two_step():
  73. import random
  74. # Uses string_frac()
  75. a = random.randint(1,11)
  76. b = random.randint(-7,12)
  77. c = random.randint(2,36)
  78. print(a, "x + ", b, " = ", c)
  79. print("Round answer to two decimal places")
  80. ans_in = input("x = ")
  81. answer = (c-b)/a
  82. # test
  83. if round(string_frac(ans_in),2)==round(answer,2):
  84. print("Correct! \n")
  85. else:
  86. print("Try again")
  87. print("The correct answer is ", answer, "\n")
  88. # test loop
  89. for a in range(2):
  90. one_step_add()
  91. one_step_subtract()
  92. one_step_mult()
  93. one_step_div()
  94. two_step()
  95. print(" ")
  96. two_step()
  97. two_step()