Can you help me with this?


#1

a=int(input ("escribe un numero:"))
numero=bin(a)

n=int(input("escribe un numero entero:"))
def invierte_bit(numero,n):
    mask=(0b1<<(n-1))
    result=numero^mask
    return result
print (invierte_bit(numero,n))

The problem is that when I try to run this, the program tells me
"Traceback (most recent call last):
File "C:/Python34/1.py", line 10, in
print (invierte_bit(numero,n))
File "C:/Python34/1.py", line 8, in invierte_bit
result=numero^mask
TypeError: unsupported operand type(s) for ^: 'str' and 'int'"

I donĀ“t Know how to solve it


#2

This is not part of an exercise? It looks like python3, ran on windows. Take a look at this 3 lines of code:

a=int(input ("escribe un numero:"))
numero=bin(a)
print(type(numero))

(the first two lines are yours, i added the third), i don't know why you call bin(), and i am not sure what it does, but it casts your integer to string, which is something you don't want to have


#3

Thank you for answering me
I want to do this (see the picture) but introducing the numbers from the keyboard with the order "input".
I am studing python here and I like it a lot (I wish the course never ended) but really I need to study python 3 and I usually repeat the exercises with the new version; sometimes there are some differences


#4

error and problem resolved?