number question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

number question

I need to knock off the last digit of any user inputed number, Eg. 50 becomes 5 84884 becomes 8488 and so on, I need help on the codin as I don't know were to begin, thank you.

13th Jul 2017, 4:58 AM
Kai Simons
Kai Simons - avatar
7 Answers
+ 4
Divide the number by 10. Meanwhile, if it's a single digit number then the number itself is the last digit.
13th Jul 2017, 5:19 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
User input return a string, so slicing is the best way, as suggested by @J Prak, without need to convert it to string in this particular case: str_num = input('enter a number: ') print(str_num[:-1]) Anyway, you may want to verify that the user entry is a valid number, and/or use the 'digitless' number in calculation: def get_num(): str_num = input('enter a number: ') try: num = int(str_num) except: print('not valid number!') return None return int(str_num[:-1]) num = None while num is None: num = get_num()
13th Jul 2017, 5:43 AM
visph
visph - avatar
+ 2
you can use string slice. convert the integer to string. string slice will remove the last character in the string and again you can convert the string to integer. >>num = 50 >>print(int(str(num)[:-1])) >> 5 This will work for both integer and string to knock of the last character.
13th Jul 2017, 5:11 AM
J Prak
J Prak - avatar
+ 2
What Rrestoring faith said num // 10
13th Jul 2017, 5:26 AM
David Ashton
David Ashton - avatar
0
Well not just 50, I need it to work with any number
13th Jul 2017, 5:27 AM
Kai Simons
Kai Simons - avatar
0
Thank you J Prak, it seems to be working
13th Jul 2017, 5:32 AM
Kai Simons
Kai Simons - avatar
0
Thanks visph, haven't thought of that... I will try it out!
13th Jul 2017, 5:45 AM
Kai Simons
Kai Simons - avatar