gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
python编程取出十进制数的每一位
- User
python编程取出十进制数的每一位
- Assistant
代码如下:
num = 12345 # 要取出的十进制数 digits = [] # 存放每一位的列表 while num > 0: digit = num % 10 # 取出最低位 digits.append(digit) # 将最低位加入列表 num //= 10 # 去掉最低位 digits.reverse() # 反转列表,得到正确的顺序 print(digits) # 输出结果输出结果为:
[1, 2, 3, 4, 5]注:代码中用到了整除运算符
//。num//10表示将num除以10,然后向下取整,得到一个整数。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.