Learning python, the child shouted that let me give the export computation, so wrote a generation mental arithmetic problem of the program, I feel very humble, looking forward to the great God to modify, any of your suggestions are my greatest encouragement!
#!/usr/bin/env python
From operator Import Add,sub,imul
From random import Randint,choice,uniform
Import OS
ops={' + ': Add, '-': Sub, ' * ': Imul}
def floattoint (numlist): ' Converts floating-point numbers with decimal zeros to integers and looks good for typeset output.
For I in range (len (numlist)):
If Numlist[i]*10%10==0:numlist[i]=int (Numlist[i])
Return numlist
Def doprob (): ' Generate a mental arithmetic question, return the answer and the question string
Step=randint (2,3) ' randomly generated number of numbers in a mental arithmetic problem
If step==2:
Op=choice (' +-* ') ' randomly generated calculation symbols
If op== ' * ': ' If it is multiplication, produce a maximum of two decimal places multiplied by an integer of the mental arithmetic problem (kids don't like it too hard)
Nums=[round (Uniform (1,100), Choice ((0,2))), Randint (1,10)]
ANS=OPS[OP] (*nums)
else: ' If you add and subtract, produce a mental arithmetic problem with up to two decimal places
Nums=[round (Uniform (1,100), Choice ((0,2))) for I in range (step)]
Nums.sort (Reverse=true)
ANS=OPS[OP] (*nums)
Nums=floattoint (nums) ' Remove the last 0 of floating-point numbers
Pr= '%s%s%s= '% (nums[0],op,nums[1])
else: ' Count three, it's just a plus minus.
Op1=choice (' +-')
Op2=choice (' +-')
Nums=[round (Uniform (1,100), Choice ((0,2))) for I in range (step)]
ANS=OPS[OP1] (*nums[:2])
ANS=OPS[OP2] (* (ans,nums[2]))
Nums=floattoint (Nums)
Pr= '%s%s%s%s%s= '% (nums[0],op1,nums[1],op2,nums[2])
If Ans*10%10==0:ans=int (ans)
Return ANS,PR
def main ():
Makenum=int (raw_input (' please input the number of generated problems: '))
Gennum=0
Try
Filename= ' Kousuan%s.txt '%makenum
Fmake=open (filename, ' W ')
Filepath= '%s\%s '% (OS.GETCWD (), filename)
Except Exception,e:
print ' File Open err: ', E
Return None
For I in Range (makenum-1):
Problem=doprob ()
If problem[0]>=0:
Pout= '%s '%problem[0]+ ' * (10-len (str (problem[0))) + '%s\n '%problem[1]
Fmake.write (pout) ' Put the title in the file
Gennum+=1
Fmake.close
print ' maked%s arithmetic '%gennum
print ' The result ' in%s '%filepath ' gives the directory where the file is located
If __name__== ' __main__ ':
Main ()
First program for children: generate a txt mental arithmetic problem file