3

i am totally new to python and I have this code snippet of c++ :

do
    {
    cout << "Make sure the number of digits are exactly 12 : ";
    cin >> input ;
    } while((input.length()) != 12 );

How do I change this part to python ? I have tried this so far , I don't get what the right syntax or logic flow it will be . This is what I have :

while True:
  print("Make sure the number of digits are exactly 12 : ")
  input = raw_input()
  check = len(input)
  if check != 12
  break

The above part is solved !

Also , another c++ snippet that is : input is string

for (int i = 0; i < 12 ; i++)
    {
     code[i] = input.at(i) - '0';
    }

I cannot figure out how to change this part to python code

code[i] = input.at(i) - '0';

So, the problem i am having is I can't figure out how to initialize the array

int code[12] ;

How should that be in python so the i can execute this piece of code ! as given :

   int code[12] ;
    for (int i = 0; i < 12 ; i++)
      {
        code[i] = input.at(i) - '0';
      }
thestralFeather7
  • 529
  • 2
  • 10
  • 28
  • Note that 'translating' code from one language to another doesn't really work well, as things that make a lot of sense in one language can be inefficient and unreadable in another. – Gareth Latty Feb 08 '13 at 01:32
  • Also, if you have two questions, please post them separately instead of tacking it on to a question. It will get more people to answer, and make it more useful as a resource for others. – Gareth Latty Feb 08 '13 at 01:33

3 Answers3

5

First off, do..while is not in Python

For your first question:

while True:
  print "Make sure the number of digits are exactly 12 : "
  x = input()

  if len(str(x)) == 12:
    break

Python is sensitive to whitespace, and the methods are managed with tabs and spaces instead of brackets. Also you were missing a colon.

For your second question, the code looks like you're taking the character and converting it into the digit. You can simply do a type cast:

for i in range(12):
  code[i] = int(x[i])
Community
  • 1
  • 1
Phil
  • 6,686
  • 2
  • 19
  • 25
3

For the first code snippet, you can change:

print("Make sure the number of digits are exactly 12: ")
input = raw_input()

To:

input = raw_input("Make sure the number of digits are exactly 12: ")

You also don't need the check variable, instead just do:

if len(input) == 12:
  break

Notice how after the IF statement I include an : (the equality test must also be ==, not !=). Then, anything further indented after the decision is executed if the condition is True.

For the second code snippet, you can convert from integer to string (and string to integer) using the int() and str() functions. E.g.

>>> a = '012345678912'
>>> len(a) == 12
True
>>> b = int(a)
>>> print b
12345678912
>>> str(b)
'12345678912'
mfjones
  • 739
  • 4
  • 16
1
do
    {
    cout << "Make sure the number of digits are exactly 12 : ";
    cin >> input ;
    } while((input.length()) != 12 );

int code[12] ;
    for (int i = 0; i < 12 ; i++)
        {
        code[i] = input.at(i) - '0';
        }

translates to

while True:
    input = raw_input("Make sure the number of digits are exactly 12 : ")
    if len(input) == 12:
         break
code = []
for ind in range(0,12):
    code.append(ord(input[ind]) - ord('0'))

There are simpler ways of parsing a string of digits to their constituent values in python, such as

code.append(int(input[ind]))

the translation I've provided is agnostic to the purpose of the code [could include letters etc] though

the variable 'code' in the python is a list rather than an array of course

Benji
  • 21
  • 1