![](/img/trans.png)
[英]Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
[英]TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'set'
问题:不使用任何字符串方法,尝试打印以下内容: 123...n 请注意,“...”表示其间的连续值。
示例 n=5 打印 12345。
我的解决方案
n = int(input())
sum=0
i=n
while i>0:
sum=sum + i*(10**{n-i})
i -= 1
print(sum)
首先:如果 n=0, {ni}
将评估为{-1}
,因为{x}
是在 python 中表达集合的方式
第二:您要求打印数字字符串的方法,但没有字符串操作(因此所有连接都应该在两个整数之间进行)。 这里我假设接受的输入只能是正数
例如:
在学习解决此类“挑战”问题时,最好进行测试驱动:编写一个可以正常工作的简单程序,然后与生成的预期结果进行比较/断言
这是生成 output(使用字符串操作)的正确但不可接受的方法:
inp = int(input())
expected = ""
for i in range(1, inp+1):
expected = expected + str(i)
print(expected)
然后尝试逐步解决它:仅假设单个数字输入。 在这里,我们的想法是,为了将一个数字放在另一个数字旁边,我们需要将第一个数字乘以 10,然后将下一个数字乘以 1。因此,将其乘以 10 的幂的解决方案已经在正确的轨道上
现在我们可以写:
inp = int(input())
result = 0
for i in range(1, inp+1):
power_of_ten = 10**(inp-i)
print("pot", power_of_ten)
result = result + (i*power_of_ten)
print("r", result)
print(result)
output:
5
pot 10000
r 10000
pot 1000
r 12000
pot 100
r 12300
pot 10
r 12340
pot 1
r 12345
12345
此时,我们可以尝试断言我们的 output 是否与我们生成的 output 相同(使用字符串操作的那个):
inp = int(input())
result = 0
for i in range(1, inp+1):
power_of_ten = 10**(inp-i)
result = result + (i*power_of_ten)
print(result)
expected = ""
for i in range(1, inp+1):
expected = expected + str(i)
print(expected)
assert(result == int(expected))
print("assertion passed")
output:
5
12345
12345
assertion passed
但如果我们使用两位输入,output 将不再正确:
12
123456790122
123456789101112
Traceback (most recent call last):
File "/tmp/c.py", line 14, in <module>
assert(result == int(expected))
AssertionError
所以,如果我们必须在输入 12 时 output 123456789101112 ,那么我们需要一个数学 function (不是字符串函数)来计算数字中的位数:
这样的 function 称为对数 function:例如:
math.floor(math.log(i, 10)) + 1
首先我们尝试对输入以 10 为底,然后我们将结果取底(这样结果就不是小数/小数); 然后我们加 1
这是包含它的代码:请注意,为简单起见,我们正在向后循环(例如:12,11,10,9..1)
import math
inp = int(input())
result = 0
pad = 0
for i in range(inp, 0, -1):
result = result + i*10**pad
pad = pad + math.floor(math.log(i, 10)) + 1
print(result)
expected = ""
for i in range(1, inp+1):
expected = expected + str(i)
print(expected)
assert(result == int(expected))
print("assertion passed")
在这里我添加了一个变量pad
,它将包含下一次迭代要添加的 pad 的数量,例如:input=5
当输入=12
output:
$ python3 /tmp/a.py
5
12345
12345
assertion passed
$ python3 /tmp/a.py
12
123456789101112
123456789101112
assertion passed
所以最终的代码是:
import math
inp = int(input())
result = 0
pad = 0
for i in range(inp, 0, -1):
result = result + i*10**pad
pad = pad + math.floor(math.log(i, 10)) + 1
print(result)
n = int(input())
s, m = 0, 1
for i in range(n, 0, -1):
s += i*m
m *= 10
print(s)
chepner 的评论和Kristian 的回答解决了您收到该错误的原因。
我想知道,你甚至需要做任何算术吗? 这提供了一个字符串作为print
的参数,但实际上并不使用任何字符串方法(例如join
)。 有人可能会争辩说,这遵循了“不使用任何字符串方法......”的字母(如果不是精神的话)。
n = int(input("Enter a number: "))
# Generate a list of the numbers from 1 through n.
numbers = list(range(1, n + 1))
# Print out all the numbers, without spaces between them.
print(*numbers, sep='')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.