python中isinstance怎么用_python – 我如何正确使用isinstance()

def convBin():

cont = []

rest = []

dev = []

decimal = []

print("Give me a number: ")

valor = input()

if isinstance(valor, int):

while valor > 0:

z = valor // 2

resto = x%2

valor = valor // 2

cont.append(z)

rest.append(resto)

cont.reverse()

rest.pop()

dev.append(cont[1])

for i in rest:

dev.append(rest[i])

print(" ")

print("Lista de devoluciones: ")

print(dev)

print("")

elif isinstance(valor, float):

a = valor // 1

b = valor % 1

while a > 0:

z = a // 2

resto = a%2

a = a // 2

cont.append(z)

rest.append(resto)

cont.reverse()

rest.pop()

dev.append(cont[1])

for i in rest:

dev.append(rest[i])

print("How many decimals do you want?")

num = input()

while num > 0:

dec = b * 1

dec2 = dec//1

dec %= 1

decimal.append(dec2)

print("Full part: ")

print(dev)

print("Decimal part:")

print(num)

else:

print("An error has appeared")

我正在自己学习Python,所以我知道我在代码中犯了大错.欢迎任何建议.

此代码用于二进制转换器.

有一个isinstance()的问题.当我尝试代码时,在键盘读取的那一刻,它忽略了“if”,它直接转到“else”.

例如:

1. It asks you a number.

2. It goes to the first if and compare the x type with int(for some reason it is false).

3. It goes to the `elif` and does the same(check if its float).

4. Both are false so it goes to else and prints the error.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值