+ 2

list of digit with mathematics operation (loop)

context is, i want to split all number's digit like list, string function/method with loop and math operator solution is: 1) let's say we want to separate this number 123 with % operator by 10. like this, we will get last digit of number >>> x = 123 >>> a = x % 10 (get 3) 2) Divide the number you want to split by 10 to get only integers (// operator) >>> x = x // 10 (get 2) 3) bring the numbet from step 2 back to step 1 >>> b = x % 10 (12 % 10 = 2) and repeat the loop my code: n = int(input("input number: ")) remain = n digit = 0 result = ' ' while remain != 0: degit = remain % 10 if result == ' ': result = str(digit) else: result = str(digit) + ', ' + result remain = remain // 10 print(f"list of digit: {result}") input >>> 5678 #expected output list of digit: 5, 6, 7, 8 #my output list of digit: 0, 0, 0, 0 question is why my output is 0, 0, 0, 0. can someone correct my code please? i have no idea why ...

24th Dec 2021, 6:57 AM
キャサリン
キャサリン - avatar
2 Antworten
+ 5
Maybe it is the little typo in the first line of your while loop degit should be digit
24th Dec 2021, 7:28 AM
Oma Falk
Oma Falk - avatar
+ 1
Oma Falk oh thank you! I didn't spot this little mistake. thank you again!
24th Dec 2021, 8:07 AM
キャサリン
キャサリン - avatar