Python implements children's arithmetic games

Source: Internet
Author: User
Tags arithmetic

Randomly select numbers and an arithmetic function to display the problem and verify the results. The result is given after 3 bad attempts, and will continue to run when the user enters a correct answer.

# -*- coding: utf-8 -*-from operator import add, sub                   #从  operator  and  random  module, import the functions we will use        from random import  randint, choiceops = {' + ': add,  '-': sub}                      #定义全局变量   A collection that contains the operators and the functions associated with them (dictionary) maxtaries = 3                                    #定义全局变量   How many times the user has the opportunity to try to give an answer to an integer variable Def doprob ():                                       #定义此程序的核心程序     op = choice (' +-')                                #随机选择一个操作     nums = [ Randint (1, 10)  for i in range (2)]     #随机生成两个操作数      nums.sort (reverse=true)                         #为了避免减法问题中的负数问题, sort the two operands down to the next      ANS = OPS[OP] (*nums)                            #调用一个数学函数计算出正确的解      pr =  '%d %s %d = '  %  (nums[0], op, nums[1])      #生成   Prompts the user to calculate the equation     oops = 1                                        #定义   Counters for user-attempted opportunities     while True:         try:             if int (Raw_input (pr))  == ans:         #判断   User-entered answers and   correct answers compare                  print  ' correct '                     #输出提示信息                  break                              #退出循环              if oops == maxtaries:                  #当用户尝试次数等于用户最大尝试次数时                  print  ' answer\n%s%d '% (Pr,ans)       #将等式和   Correct answer output                  break             else:                                   #在其他情况下                  print  ' incorrect...  Try again '    #输出提示信息                 oops  += 1                          #用户尝试机会的计数器加一          except (Keyboardinterrupt, eoferror, valueerror):          #捕捉用户的错误输出             print  ' invalid  input... try again '     #输出提示信息def  main ():     while  true:        doprob ()                                   #调用核心函数         try:         &Nbsp;   opt = raw_input (' again?[ Y] '). Lower ()      #读取用户输入的参数   Lowercase If it is uppercase              if opt and opt[0] ==  ' n ':                 #当用户输入n   or  N  exit user cycle                  break         except (Keyboardinterrupt, eoferror):             breakif __name__ ==  ' __main__ ':     main ()


Python implements children's arithmetic games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.