Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report