Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+9 votes

I tutor fifth grade math and I wanted to make a code that can generate an expression in the form:
a ( b + c ), where a, b, and c are random numbers of my choosing and the + can be either + or -. I also would like to be able to choose the number of equations generated.
Does anybody have any suggestions to help me out?

For example:

How many equations? 3
5 ( 4 - 2 )
7 ( 8 + 7 )
1 ( 9 + 2 )

asked in CSC201 Spring 2021 by (1 point)

2 Answers

+5 votes

Suppose that 'equation' is the variable for the numbers of equations.
You will need a for loop in order to print the equation.
example:

for i in range (1,equation+1):

(you will need to run the print statement 'equation' times)

Inside the loop:
Because you want 3 different numbers for each equation, you will want to have 3 input statements for those numbers each time the loop runs.
To choose between "+" and "-", you can use the random.choice.
example:

random.choice(['+','-'])

The final step is the print statement.

answered by (1 point)
+2 votes

Hi,
I think i know what you can do make different random statement for every equation in a for look, for example:
for math in range(1,equation+1)

 yay = random.randrange(1,11)
 yay2 = random.randrange(1,11)
 yay3 = random.randrange(1,11)
 print(yay,"(",yay2,"-",yay3,")")

I hope this makes sence!!!!

answered by (1 point)
+2

Ok, i forgot about the random + or -, so ya just do as Ngoc Le said!!

...