Home > other >  Please ask! Python to write simple computer can output the correct answer but there is an error
Please ask! Python to write simple computer can output the correct answer but there is an error

Time:10-16

Function is to enter a operation) (addition, subtraction, multiplication, and division of the results, not the eval, as far as possible need not regular expressions, digital contain floating point Numbers, binary, octal, hexadecimal
Below is the code I wrote, other tests are passed, but negative complains/output the answer is right
All kinds of changes... Still not solve the problem...

Code:

Formula=input (" please enter want arithmetic formula: ")
Op=[' + ', '-' and '*', '/']
Formula=formula [:]. Replace (', ')
For I, opr in enumerate (formula) :
If opr in op and I!=0:

If formula [0]=='-' :
Num1=- float (formula [1: I])
Elif formula [2-0]=='0 x:
Num1=formula [I] 2:
Num1=int (num1, 16)
Elif formula [2-0]=='0 b:
Num1=formula [I] 2:
Num1=int (num1, 2)
Elif formula [2-0]=='0 o:
Num1=formula [I] 2:
Num1=int (num1, 8)
The else:
Num1=float (formula [0: I])

If formula [I + 1)=='-' :
Num2=- float (formula [I + 2:])
Elif formula [I + 1, I + 3]=='0 x:
Num2=formula/I + 3:
Num2=int (num2, 16)
Elif formula [I + 1, I + 3]=='0 b:
Num2=formula/I + 3:
Num2=int (num2, 2)
Elif formula [I + 1, I + 3]=='0 o:
Num2=formula/I + 3:
Num2=int (num2, 8)
The else:
Num2=float (formula [I + 1:])

If opr==op [0] :
Print (' {: 2 f} '. The format (num1 + num2))
Elif opr==op [1] :
Print (' {: 2 f} '. The format (num1 - num2))
Elif opr==op [2] :
Print (' {: 2 f} '. The format (num1 * num2))
Elif opr==op [3] :
Print (' {: 2 f} '. The format (num1/num2))




Debugging results:



Baidu search, also can see symbols cannot be transformed, but won't be changed,,, slice scope if adjustment is not the correct value, can ask do ah,,,

CodePudding user response:

their busy along while still can't solve...

CodePudding user response:

 formula=input (" please I want computing formula:") 
Op=[' + ', '-' and '*', '/']
Formula=formula [:]. Replace (', ')
For I, opr in enumerate (formula) :
If opr in op and I!=0:
If formula in op: [I - 1]
The continue

If formula [0]=='-' :
Num1=- float (formula [1: I])
Elif formula [2-0]=='0 x:
Num1=formula [I] 2:
Num1=int (num1, 16)
Elif formula [2-0]=='0 b:
Num1=formula [I] 2:
Num1=int (num1, 2)
Elif formula [2-0]=='0 o:
Num1=formula [I] 2:
Num1=int (num1, 8)
The else:
Num1=float (formula [0: I])

If formula [I + 1)=='-' :
Num2=- float (formula [I + 2:])
Elif formula [I + 1, I + 3]=='0 x:
Num2=formula/I + 3:
Num2=int (num2, 16)
Elif formula [I + 1, I + 3]=='0 b:
Num2=formula/I + 3:
Num2=int (num2, 2)
Elif formula [I + 1, I + 3]=='0 o:
Num2=formula/I + 3:
Num2=int (num2, 8)
The else:
Num2=float (formula [I + 1:])

If opr==op [0] :
Print (' {: 2 f} '. The format (num1 + num2))
Elif opr==op [1] :
Print (' {: 2 f} '. The format (num1 - num2))
Elif opr==op [2] :
Print (' {: 2 f} '. The format (num1 * num2))
Elif opr==op [3] :
Print (' {: 2 f} '. The format (num1/num2))

CodePudding user response:

Thank you bosses (? 'omega `?) ! finally find the problem! Is the second minus sign can continue the loop, as minus minus the continue there detected will skip to the next round of cycle. That's really wonderful! Thank you so much!
  • Related