[英]How can I separate a double digit to two single digits with python? e.g. 15 -> 1, 5
def plus_cycles():
n = int(input("Write here the number that you want. (1~99)\n"))
while True:
if n < 10:
a = "n" + "0"
In this code, I want a to be separated with two single digits.在这段代码中,我希望 a 用两个个位数分隔。 eg 15 -> 1, 5 or [1, 5]例如 15 -> 1, 5 或 [1, 5]
The divmod()
method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. divmod()
方法接受两个数字并返回一对数字(一个元组),由它们的商和余数组成。 For integers, the result is the same as (a // b, a % b)
.对于整数,结果与(a // b, a % b)
相同。
>>>> divmod(15, 10)
(1, 5)
>>> 15 // 10
1
>>> 15 % 10
5
You Could do something like this:你可以这样做:
>>> intToList = lambda number : [int(i) for i in list(str(number))]
>>> intToList(15)
[1, 5]
list(str(15))
Will return a List of Chars like: list(str(15))
将返回一个字符列表,例如:
>>> list(str(15))
['1', '5']
So we iterate through them with a for loop to turn them back ino integers.所以我们用一个 for 循环遍历它们,把它们变成整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.