Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write code to randomly pick out 10 questions and calculate the correct rate

Shulou Source: shulou.com Published: 2022-06-03 05:46:49 10月01日 Update

This article mainly explains "how to write code to randomly generate 10 questions and calculate the correct rate". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "how to write code to achieve random 10 questions and calculate the correct rate"!

Problem description

Describe in words what the problem is to solve.

Example: randomly select 10 questions and calculate the correct rate

Enter: your result

Output: whether you are correct and give the next question, calculate the correct rate after 10 questions

Solution

The +, -, *, / four arithmetic operation symbols are randomly selected, N1 represents randomly selecting a number within 100 numbers, N2 represents randomly selecting a number within 1 to 100, N2 may be taken as the denominator, so it can not be taken to 0, other ranges can be changed, and arranged into an arithmetic formula in the form of formatted output, 10 questions are cycled out with for loop.

two。 Judge the randomly selected symbol, if it is the corresponding symbol, the answer is calculated by the corresponding symbol in the middle of the corresponding number, and this answer represents the correct answer.

3. Set an output, this output is your answer, if you choose 123, you will quit, if you continue to answer, continue to calculate your math formula and write your results, and judge whether your results are equal to the correct results. If it is not equal, go to the next question. If it is equal, enter the next question and count the correct number of questions. After 10 questions, calculate your correct rate in 10 questions.

Listing 1 DFS solves the summation problem from 1 to 100 Python code

Courier New font, 23 lb line spacing

Import random

Count = 0

For i in range (10):

Fuhaolist = ['+','-','*','/']

Fuhao = random.choice (fuhaolist)

N1 = random.randint (0100)

N2 = random.randint (1100)

Print (f'{N1} {fuhao} {N2} =')

# calculation accuracy

If fuhao ='+':

Asw = N1 + N2

Elif fuhao ='-':

Asw = N1-N2

Elif fuhao ='*':

Asw = N1 * N2

Elif fuhao ='/':

Asw = round (N1 / N2)

Your_asw = eval (input ('Please enter the answer, press 123 input to exit))

If your_asw = = asw:

Count + = 1

Elif your_asw = = 123:

Print ('exit')

Break

Else:

Print ('wrong answer')

Percent = (count/10) * 100

Print (you have answered {count} questions correctly, with a correct rate of {percent}%')

Thank you for your reading, the above is the content of "how to write code to achieve random 10 questions and calculate the correct rate". After the study of this article, I believe you have a deeper understanding of how to write code to achieve random 10 questions and calculate the correct rate, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Accuracy code answer number symbol result question output together representative learning content loop input selection yes denominator font that is form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn Shulou Tech Info Xiaomi MySQL