How can I limit the amount of digits in an input?
You have to convert the int to a string because int does not have a length property. Also You were checking if the digit was longer than 1 for a twice so I switched the SECOND NUMBER check to b
print('Please enter your 10 digit number')
a = raw_input("FIRST NUMBER: ")
if len(a) > 1:
print ("Error. Only 1 digit allowed!")
a = int(a)
aa = (a*10)
b = raw_input("SECOND NUMBER: ")
if len(b) > 1:
print ("Error. Only 1 digit allowed!")
b = int(b)
bb = (b*10)
Or more simply:
You could ask for the number and keep asking until the length is 10 and the input is a number
num = raw_input('Please enter your 10 digit number:')
while len(num) != 10 or (not num.isdigit()):
print 'Not a 10 digit number'
num = raw_input('Please enter your 10 digit number:')
num = int(num)
print 'The final number is: ', num
Kai McClymont
Updated on June 07, 2022Comments
-
Kai McClymont almost 2 years
I'm a student doing a computer science course and for part of the assessment we have to write a program that will take 10 digits from the user and used them to calculate an 11th number in order to produce an ISBN. The numbers that the user inputs HAVE to be limited to one digit, and an error message should be displayed if more than one digit is entered. This is the code that I am using:
print('Please enter your 10 digit number') a = int(input("FIRST NUMBER: ")) aa = (a*11) if len(a) > 1: print ("Error. Only 1 digit allowed!") b = int(input("SECOND NUMBER: ")) bb = (b*10) if len(a) > 1: print ("Error. Only 1 digit allowed!")
ect.
I have to keep the inputs as integers so that some of the calculations in the rest of the program work, but when I run the program, an error saying "object of type 'int' has no len()". I'm assuming that it is referring to the fact that it is an integer and has no length. Is there any way that I can keep 'a' as an integer but limit the length to 1 digit?
(Also I understand that there is probably a more efficient way of writing the program, but I have a fairly limited knowledge of python)
-
a p about 9 yearsMight be better to check before converting the input to
int
- all thisstr(int(str))
stuff is a bit silly. -
smci about 9 years
aa = (a*11)
andbb = (b*10)
are bogus, they force user to input one digit then multiply it by 10 or 11; instead of iterating 10 or 11 times on the call toinput()
-
NDevox about 9 yearsThis assumes the user cannot make a mistake which is bad practice
-
heinst about 9 years@smci I was wondering why he put that in there. I added a more simple solution too
-
Christian Witts about 9 yearsHence the note to wrap the int call in a try/except block below the code snippet.
-
NDevox about 9 yearsif non-integer. what if the user enters the wrong int.
-
heinst about 9 yearsI see you looked at my idea :p
-
Christian Witts about 9 yearsThey shouldn't really be inputting 1 character at a time, but be allowed to enter the 10 digits of the ISBN, which only then do you process. If you are assuming the user is going to be entering the incorrect values the whole time, then you should have a multi-step process asking them to input the character, then pressing y/n if that was indeed the correct one. If you're saying that because my snippet only has 1 call to
getch()
and not 10, it's because that is an exercise best left up to the OP. -
NDevox about 9 years@Heinst hah, kind of yes and no. I was going to write it originally, then saw he wanted individual digits and thought not, then thought I should give a complete answer (and realised you had also mentioned it). I can remove and give you credit if you want? I just like having everything in one place.
-
NDevox about 9 yearsMy point is a user can enter an incorrect digit, then realise, and delete before pressing enter. with your answer, they can't.
-
heinst about 9 yearsnah thats alright i was just messing around I don't care :p
-
smci about 9 years@ChristianWitts nice, but please write the complete code. When you add testing for integerness, you'll also need to add a loop.
-
Christian Witts about 9 yearsThe question was titled "How can I limit the amount of digits in an input?" with the OP wanting to only read in 1 character at a time, I fail to see how writing a complete application is needed.