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.
7 ответов
+ 4
Divide the number by 10.
Meanwhile, if it's a single digit number then the number itself is the last digit.
+ 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()
+ 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.
+ 2
What Rrestoring faith said
num // 10
0
Well not just 50, I need it to work with any number
0
Thank you J Prak, it seems to be working
0
Thanks visph, haven't thought of that... I will try it out!