[英]TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'set'
Question: Without using any string methods, try to print the following: 123...n Note that "..." represents the consecutive values in between.问题:不使用任何字符串方法,尝试打印以下内容: 123...n 请注意,“...”表示其间的连续值。
Example n=5 Prints 12345.示例 n=5 打印 12345。
my solution我的解决方案
n = int(input())
sum=0
i=n
while i>0:
sum=sum + i*(10**{n-i})
i -= 1
print(sum)
First: {ni}
will evaluate to {-1}
if n=0 since {x}
is way to express a set in python首先:如果 n=0,
{ni}
将评估为{-1}
,因为{x}
是在 python 中表达集合的方式
Second: you're asking for method to print numeric string, but no string operation (so all concatenation should be done in addition between two integers).第二:您要求打印数字字符串的方法,但没有字符串操作(因此所有连接都应该在两个整数之间进行)。 Here I'm assuming that the accepted input can only be positive number
这里我假设接受的输入只能是正数
eg:例如:
When learning to solve such 'challenge' problem, it's better to do it test driven: write a simple program that just work, then compare/assert with generated expected result在学习解决此类“挑战”问题时,最好进行测试驱动:编写一个可以正常工作的简单程序,然后与生成的预期结果进行比较/断言
this is the correct but not acceptable way to generate the output (with string operation):这是生成 output(使用字符串操作)的正确但不可接受的方法:
inp = int(input())
expected = ""
for i in range(1, inp+1):
expected = expected + str(i)
print(expected)
Then try to solve it gradually: assume single digit input only.然后尝试逐步解决它:仅假设单个数字输入。 Here we got the idea that in order to place a number beside other number, we need to multiply first number by 10, then next number by 1. So your solution for making it multiplied by power of ten is already on correct track
在这里,我们的想法是,为了将一个数字放在另一个数字旁边,我们需要将第一个数字乘以 10,然后将下一个数字乘以 1。因此,将其乘以 10 的幂的解决方案已经在正确的轨道上
now we can write:现在我们可以写:
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: output:
5
pot 10000
r 10000
pot 1000
r 12000
pot 100
r 12300
pot 10
r 12340
pot 1
r 12345
12345
at this point, we can try to assert if our output is the same with our generated output (the one that use string operation):此时,我们可以尝试断言我们的 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: output:
5
12345
12345
assertion passed
but if we use two digit input, the output will no longer be correct:但如果我们使用两位输入,output 将不再正确:
12
123456790122
123456789101112
Traceback (most recent call last):
File "/tmp/c.py", line 14, in <module>
assert(result == int(expected))
AssertionError
so, if we have to output 123456789101112 when we input 12, then we need a mathematical function (not a string function) that can count the number of digit in a number:所以,如果我们必须在输入 12 时 output 123456789101112 ,那么我们需要一个数学 function (不是字符串函数)来计算数字中的位数:
such function is called logarithm function: eg:这样的 function 称为对数 function:例如:
math.floor(math.log(i, 10)) + 1
first we try to logarithm the input to base 10, then we floor the result (so that the result is not a decimal/fractional number);首先我们尝试对输入以 10 为底,然后我们将结果取底(这样结果就不是小数/小数); then we add 1
然后我们加 1
here is the code that incorporate that: note that for simplicity, we're looping backward (eg: 12,11,10,9..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")
here I added a variable pad
that will contain the number of pad to be added on next iteration, eg: input=5在这里我添加了一个变量
pad
,它将包含下一次迭代要添加的 pad 的数量,例如:input=5
when input=12当输入=12
output: output:
$ python3 /tmp/a.py
5
12345
12345
assertion passed
$ python3 /tmp/a.py
12
123456789101112
123456789101112
assertion passed
so the final code is:所以最终的代码是:
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's comment and Kristian's answer address why you're getting that error. chepner 的评论和Kristian 的回答解决了您收到该错误的原因。
I wonder though, do you even need to do any arithmetic at all?我想知道,你甚至需要做任何算术吗? This supplies a string as a parameter to
print
, but doesn't actually use any string methods (such as join
).这提供了一个字符串作为
print
的参数,但实际上并不使用任何字符串方法(例如join
)。 One could argue this follows the letter (if not the spirit) of "Without using any string methods..."有人可能会争辩说,这遵循了“不使用任何字符串方法......”的字母(如果不是精神的话)。
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.