Home > other >  Python is urgent! (save the children)
Python is urgent! (save the children)

Time:09-17

Write a file called the except - multiple. Py program, to complete the following three expressions computing:
A/(a - b - 1)
Math. SQRT (a 2 - b * * * * 2)
A * * b
Requirements: a, b two variable number of input by the user:
They can be a floating point number;
A and b must be greater than 20;
Capture the Ctrl + Z key;
If there is wrong, let users continue to input;
Allows the user to press Ctrl + C interrupt program, but it must be output tip;
Every mistake all output cumulative number of mistakes, and finally successful completion of the task, if had made a mistake, will output a wrong number,
May run results is as follows:
Number 1:18
Number 2:19
The first number must be greater than the second number!
You made one mistake!
Number 1:19
Number 2:18
Each number must be greater than 20!
You made two mistakes!
The first number: 28
Number 2:27
Divisor cannot be zero!
You made three mistakes!
The first number: me
Number 2:19
Please use the half Angle of Arabic numerals!
You made 4 times mistakes!
The first number: ^ Z
Don't have no matter to press Ctrl + Z!
You made five mistakes!
The first number: 20000
The second number: 200
20000.000000 present (20000.000000-200.000000-1)=1.010152
Math. SQRT (20000.000000 * * * * 2) 2-200.000000=19998.999975
You can input number is too big!
Error you made six times!
The first number: 28
Number 2:22
28.000000 present (28.000000-22.000000-1)=5.600000
Math. SQRT (28.000000 * * * * 2) 2-22.000000=17.320508
28.000000 * 22.000000=68782299287045578092179575799808.000000
Error you made six times!
Mission complete!

(come and save the children)

CodePudding user response:

# coding: utf-8
The import math
Err=0
Def error (code) :
Global err
If code==1:
Print (" the first number must be greater than the second number!" )
Elif code==2:
Print (" each number must be greater than 20!" )
Elif code==3:
Print (" divisor cannot be zero!" )
Elif code==4:
Print (" please use the half Angle of Arabic numerals!" )
Elif code==5:
Print (" don't have no matter to press Ctrl + Z!" )
Elif code==6:
Print (" you have input number is too big!" )
Err +=1
Print (" you made "+ STR (err) +" wrong!" )

Def the read () :
Try:
A=input ()
Except EOFError:
Error (5)
The return - 1
Try:
B=input ()
Except EOFError:
Error (5)
The return - 1
Try:
A=float (a)
B=float (b)
Except, ValueError:
Error (4)
The return - 1
If aError (1)
The return - 1
If a<20 or b<20:
Error (2)
The return - 1
Try:
I=a/(a - b - 1)
Except ZeroDivisionError:
Error (3)
The return - 1
Y=math.h SQRT (a 2 - b * * * * 2)
Print (" 6 f} {: present ({: 6 f} - {6} f - 1)={: 6 f} ". The format (a, a, b, I))
Print (" math. SQRT ({: 6 f} 2 - {: 6} f * * * * 2)={: 6 f} ". The format (a, b, y))
If y> 10000:
Error (6)
The return - 1
X=a * * b
Print ("} {: 6 f * * {: 6 f}={: 6 f} ". The format (a, b, x))
Return 0

Def the main () :
Try:
While True:
If the read ()==0:
Break
Print (" you made "+ STR (err) +" wrong!" )
Print (" mission accomplished!" )
Return 0
Except KeyboardInterrupt:
Print (" you press CTRL + C!" )
The return - 1
Except:
Print (" unknown error ")
The return - 2

If __name__=="__main__" :
The import sys
Sys. Exit (the main ())
  • Related