Home > other >  Help: write a function to STR to int
Help: write a function to STR to int

Time:11-21

Good god, he had to write a function, want input STR type digital forced conversion to type int, but doesn't work, don't know where the problem is, should be how to modify the function, please teach a great god, the code is as follows, thanks!
Def transtoInt (a, b, c) :
Aa=a
Bb=b
Cc=c
A=int (float (aa))
B=int (float (bb))
C=int (float (cc))
Return a, b, c

CodePudding user response:

Why do you want to add float??

CodePudding user response:

Because the user may enter an integer, so I want to first converted to a float, then converted to int,

CodePudding user response:

The building Lord, how don't work? Work is quite good!
 def transtoInt (a, b, c) : 
Aa=a
Bb=b
Cc=c
A=int (float (aa))
B=int (float (bb))
C=int (float (cc))
Return a, b, c
Print (transtoInt (' 12.3 ', 45.6 ', '78'))
# as a result (12, 45, 78)

CodePudding user response:

(x, y, z)=transtoInt (' 12.3 ', 45.6 '78')
The test no problem,

CodePudding user response:

The lack of an output to receive the results returned

CodePudding user response:

You print it, application no problem

CodePudding user response:

Program there is no problem, but add a try best to prevent the input cannot be converted to the integer
the condition of the
 def transtoInt (* n) : 
Try:
N=[int (float (item)) for the item in n [0]]
Return n
Except, ValueError as e:
Print (" failure ")
  • Related