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()
.Когда я пробую код, в тот момент, когда он читает с клавиатуры, он игнорирует «если» и переходит непосредственно к «другому».
Например:
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.