简体   繁体   English

类型错误:** 或 pow() 不支持的操作数类型:'int' 和 'set'

[英]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:例如:

  • input 5, output=12345输入 5,输出=12345
  • input 12, output=123456789101112输入12,输出=123456789101112

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 (不是字符串函数)来计算数字中的位数:

  • output 2 if we input 12, 40, 99, 80 (two digit number) output 2 如果我们输入 12、40、99、80(两位数)
  • output 1 if we input 1, 5, 2 (one digit number) output 1 如果我们输入 1、5、2(一位数)
  • etc.等等

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

  • iteration=1 i=5 pad=1 result=5 (so next number, ie: 4, will be multiplied with 10^1)迭代=1 i=5 pad=1 结果=5(所以下一个数字,即:4,将乘以 10^1)
  • iteration=2 i=4 pad=2 result=45 (so next number, ie: 3, will be multiplied with 10^2)迭代=2 i=4 pad=2 结果=45(所以下一个数字,即:3,将乘以 10^2)
  • iteration=3 i=3 pad=3 result=345迭代=3 i=3 pad=3 结果=345
  • iteration=4 i=2 pad=4 result=2345迭代=4 i=2 pad=4 结果=2345
  • iteration=5 i=1 pad=5 result=12345迭代=5 i=1 pad=5 结果=12345

when input=12当输入=12

  • iteration=1 i=12 pad=2 result=12迭代=1 i=12 pad=2 结果=12
  • iteration=2 i=11 pad=4 result=1112迭代=2 i=11 pad=4 结果=1112
  • iteration=3 i=10 pad=6 result=101112迭代=3 i=10 pad=6 结果=101112
  • iteration=4 i=9 pad=7 result=9101112迭代=4 i=9 pad=7 结果=9101112
  • iteration=5 i=8 pad=8 result=89101112迭代=5 i=8 pad=8 结果=89101112
  • iteration=6 i=7 pad=9 result=789101112迭代=6 i=7 pad=9 结果=789101112
  • iteration=7 i=6 pad=10 result=6789101112迭代=7 i=6 pad=10 结果=6789101112
  • iteration=8 i=5 pad=11 result=56789101112迭代=8 i=5 pad=11 结果=56789101112
  • iteration=9 i=4 pad=12 result=456789101112迭代=9 i=4 pad=12 结果=456789101112
  • iteration=10 i=3 pad=13 result=3456789101112迭代=10 i=3 pad=13 结果=3456789101112
  • iteration=11 i=2 pad=14 result=23456789101112迭代=11 i=2 pad=14 结果=23456789101112
  • iteration=12 i=1 pad=15 result=123456789101112迭代=12 i=1 垫=15 结果=123456789101112

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.

相关问题 Python 3 TypeError:**或pow()不支持的操作数类型:“ str”和“ int” - Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' TypeError:**或pow()不支持的操作数类型:“ list”和“ int” - TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int' 类型错误:** 或 pow() 不支持的操作数类型:“str”和“int” - TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' TypeError:**或pow()不支持的操作数类型:“函数”和“整数”概率计算器 - TypeError: unsupported operand type(s) for ** or pow(): 'function' and 'int' probability calculator 类型错误:不支持 Pow 的操作数类型:第 15 行的“list”和“int” - TypeError: unsupported operand type(s) for Pow: 'list' and 'int' on line 15 **或pow()不支持的操作数类型:&#39;Entry&#39;和&#39;int&#39;? - unsupported operand type(s) for ** or pow(): 'Entry' and 'int'? **或pow()不支持的操作数类型:“函数”和“整数” - unsupported operand type(s) for ** or pow(): 'function' and 'int' “不支持 ** 或 pow() 的操作数类型:&#39;function&#39; 和 &#39;int&#39;” - "unsupported operand type(s) for ** or pow() : ' function' and 'int' " **或pow()不支持的操作数类型:&#39;method&#39;和&#39;int&#39; - unsupported operand type(s) for ** or pow(): 'method' and 'int' 将公钥传递到 rsa 解码器类型错误的问题:** 或 pow() 不支持的操作数类型:“str”和“int” - problem passing public key into rsa decoder TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM