简体   繁体   中英

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.

Example n=5 Prints 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

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
  • input 12, output=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):

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

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:

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):

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

but if we use two digit input, the output will no longer be correct:

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:

  • output 2 if we input 12, 40, 99, 80 (two digit number)
  • output 1 if we input 1, 5, 2 (one digit number)
  • etc.

such function is called logarithm function: eg:

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); then we add 1

here is the code that incorporate that: note that for simplicity, we're looping backward (eg: 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

  • iteration=1 i=5 pad=1 result=5 (so next number, ie: 4, will be multiplied with 10^1)
  • iteration=2 i=4 pad=2 result=45 (so next number, ie: 3, will be multiplied with 10^2)
  • iteration=3 i=3 pad=3 result=345
  • iteration=4 i=2 pad=4 result=2345
  • iteration=5 i=1 pad=5 result=12345

when input=12

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

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.

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 ). 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='')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM