1

So, I'm doing some practice before my school assessment, was wondering how I'd make it so that when I enter an integer with a 0 as the first value, it wouldn't convert it into an integer with no zero at the start. Here's a little extract of my program:

initialNumber = int(input("Enter your 10 digit number: "))

while len(str(initialNumber)) != 10:
    print("Next time please enter a 10 digit numerical value excluding decimals...")
    time.sleep(1)
    initialNumber = int(input("Enter your 10 digit number: "))

calculator()

I'm using Python 3.4, I understand that I could do something along the lines of this:

initialNumber = input("Enter your ten digit number")

I understand that by using this it keeps the "0", but I need to convert the string into an integer for arithmetic purposes. Any help would be greatly appreciated :)

HazzaMcJazza
  • 53
  • 1
  • 8
  • 2
    An integer does not have leading 0s. If you need to use an integer for integer type operations (multiplication for example) then you need to store it as an integer and you can't have the leading 0s... you haven't mentioned why you would want to but remember you can reformat into a string before returning your integer. – Ben Feb 11 '15 at 17:57
  • 2
    If you convert it to an integer for numeric purposes, then there are no digits. It's just a numeric value. – Barmar Feb 11 '15 at 17:57
  • 2
    A better question might be , "_when I print_ an integer, how do I pad enough zeros to the left so it's a certain length?" – Kevin Feb 11 '15 at 17:58
  • 1
    a leading zero means octal in python2 and is invalid syntax in python3 – Padraic Cunningham Feb 11 '15 at 17:58
  • 1
    possible duplicate of [Display number with leading zeros](http://stackoverflow.com/questions/134934/display-number-with-leading-zeros) – Barmar Feb 11 '15 at 17:59
  • 4
    can you explain the arithmetic purpose that you need to keep a leading zero? – Padraic Cunningham Feb 11 '15 at 18:06
  • 1
    Truth is preceding zeros have no effect on the value of the number. So why use them? – Malik Brahimi Feb 11 '15 at 18:08
  • If you want to print an integer, prefixed by zeros, use `print(str(initialNumber).zfill(10))`. If you want to test whether the input starts with a zero (for whatever reason), test that with the string that you get, and convert to int afterwards. – L3viathan Feb 11 '15 at 18:12
  • For those of you that have asked, I need to separate each digit of the value then multiply each individual digit by different consecutive values then add all of them up together. I have achieved this but I cannot do this with integers with leading 0s due to the change in length, the initial value must be 10 characters long. That's why it's still relevant for me to have leading 0s, hope that cleared things up :/ Also, I know that the value produced when mutliplying any of the numbers by 0 is 0, that still doesn't fix the length problem and this 0 still needs to be used, ughh – HazzaMcJazza Feb 11 '15 at 18:14
  • 1
    You don't want the entire string to be an integer then but each character in that string needs to be one... – Ben Feb 11 '15 at 18:17
  • Ah, that would make sense. Thank you, Ben :) – HazzaMcJazza Feb 11 '15 at 18:20

2 Answers2

3

Although personally I do not see the sense doing this. I would create a string variable to hold the leading zeros before performing math operations.

initialNumber = (raw_input("Enter your 10 digit number: "))

while len(str(initialNumber)) != 10:
    print("Next time please enter a 10 digit numerical value excluding decimals...")
    time.sleep(1)
    initialNumber = (raw_input("Enter your 10 digit number: "))

leadingZeros = (len(initialNumber) - len(str(int(initialNumber)))) * "0"

calculator()

After the arithmetic you can easily concatenate the zeros to the beginning the output.

PVNRT
  • 235
  • 1
  • 4
  • 14
0

If you want ten individual ints from the input call list on the input and map to int:

initialNumber = list(map(int,(input("Enter your 10 digit number: "))))


In [1]: initialNumber = list(map(int,(input("Enter your 10 digit number: "))))
Enter your 10 digit number: 0123456789

In [2]: initialNumber
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

while True:
    initialNumber = input("Enter your 10 digit number: ")
    if len(initialNumber) != 10 or not initialNumber.isdigit():
        print("Invalid input")
    else:
        print(list(map(int,initialNumber)))
        break

Normally you would use a try/except to check that the input is numeric but allowing negative numbers would not work so as out character count would be wrong.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321