-1

if a = 110 I want to make b = 11

if a = 120 I want to make b = 12

if a = 010 I want to make b = 1

if a = 020 I want to make b = 2

Is this achievable with any logic in a Python script?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
GoneCase123
  • 388
  • 4
  • 15
  • 1
    Yes, of course that's achievable. But this isn't a tutorial or code-writing service; [learn some Python](http://sopython.com/wiki/What_tutorial_should_I_read%3F), and actually *try to achieve it*. It seems like you just want `b` to be equal to `a` divided by ten, in which case how do you *think* you do it?! – jonrsharpe Mar 25 '17 at 08:45
  • what to do in case of 102? – Ashish Mar 25 '17 at 08:48
  • Also be aware that `020 == 16` in Python 2.x, as the leading zero means octal integer literals. Or do you mean they're strings, like `'020'` (in which case including the quotes would be helpful)? – jonrsharpe Mar 25 '17 at 08:52
  • @jonrsharpe a = 010 and b = 10 and c = a/b i am getting 0 – GoneCase123 Mar 25 '17 at 09:02
  • @Ashish my case i wont get 102 value..in my case only 110, 010,020,030,130,150 and so on – GoneCase123 Mar 25 '17 at 09:02
  • As I said, it's an octal literal. `010 == 8` (see e.g. http://stackoverflow.com/q/13431324/3001761) and, due to integer division in Python 2.x (see e.g. http://stackoverflow.com/q/1267869/3001761), `8 / 10 == 0`. Please give more context as to the actual problem you're trying to solve, give an actual [mcve]. – jonrsharpe Mar 25 '17 at 09:04
  • yes done that. check my code. – Ashish Mar 25 '17 at 09:04
  • @Ashish your answer won't work with e.g. `a = 010`. – jonrsharpe Mar 25 '17 at 09:07
  • input provided will be in string or int? – Ashish Mar 25 '17 at 09:10
  • @jonrsharpe, is there way we can convert 020 to 20. So then i will use divide rule. – GoneCase123 Mar 25 '17 at 09:14
  • @sagar well where is the 020 coming from? How are you converting it to Python datatypes? Again, give a [mcve] and provide some context. – jonrsharpe Mar 25 '17 at 09:30
  • this 020 is provide by user say a = raw_input('Enter trunk number'), these number user will provide always in terms of 010,020..100,110,120... – GoneCase123 Mar 25 '17 at 09:34
  • Well `int` uses base 10 by default, so you **don't** have `020`. This is why a [mcve] is useful; it's less ambiguous than a description. [Edit] the question, and explain why simply dividing by ten is a problem. – jonrsharpe Mar 25 '17 at 10:39
  • Thanks Jonrspare and i used int and it works fine – GoneCase123 Mar 25 '17 at 11:23
  • If you no longer have a problem I'd recommend deleting the question. – jonrsharpe Mar 26 '17 at 18:17

1 Answers1

-1

if you just want to replace all the zeros:

a = 110
b = int(str(a).replace('0',''))
print b

output

11
Ashish
  • 4,206
  • 16
  • 45
  • When it's unclear what the OP is actually asking for, as it is here, it's best not to answer until they've edited to clarify. – jonrsharpe Mar 25 '17 at 08:57