我了解您只是在学习,但仔细查看您的代码,您应该会发现很多简单的错误。
x = str (input("Enter Binario: ")) # You store the input a x, ok.
c = len (x) # You store the length of x as c, ok.
decimal = 0
b = "" # You initialize b to an empty string
for y in (b): # Then try to iterate over it - this will never loop
if ( b == "1"):
b = 1
else :
if (b =="0"):
b =0
z = c - 1
t = (2**z) * x
decimal = decimal + t
print (decimal)
所以让我们解决第一个问题。
x = str (input("Enter Binario: ")) # You store the input a x, ok.
c = len (x) # You store the length of x as c, ok.
decimal = 0
b = "" # You initialize b to an empty string
for y in x: # You iterate the characters of x, ok.
if ( b == "1"): # You check the value of b, where you should be checking the value of y
b = 1
else :
if (b =="0"):
b =0
z = c - 1
t = (2**z) * x
decimal = decimal + t
print (decimal)
所以让我们解决这个问题
x = str (input("Enter Binario: ")) # You store the input a x, ok.
c = len (x) # You store the length of x as c, ok.
decimal = 0
b = "" # You initialize b to an empty string
for y in x: # You iterate the characters of x, ok.
if y == "1": # You check the value of y, ok
b = 1 # and assign to b, but below you use x in your equation
else :
if (b =="0"):
b =0
z = c - 1
t = (2**z) * x
decimal = decimal + t
print (decimal)
所以让我们解决这个问题
x = str (input("Enter Binario: ")) # You store the input a x, ok.
c = len (x) # You store the length of x as c, ok.
decimal = 0
b = "" # You initialize b to an empty string
for y in x: # You iterate the characters of x, ok.
if y == "1": # You check the value of y, ok
x = 1 # and assign to x, ok
else:
x = 0
z = c - 1 # You are trying to get the position of the current character here so you can raise to the appropriate power, but this value will be the same each time. Instead, modify what c is.
t = (2**z) * x
decimal = decimal + t
print (decimal)
所以让我们解决这个问题
x = str (input("Enter Binario: ")) # You store the input a x, ok.
c = len (x) # You store the length of x as c, ok.
decimal = 0
b = "" # You initialize b to an empty string
for y in x: # You iterate the characters of x, ok.
if y == "1": # You check the value of y, ok
x = 1 # and assign to x, ok
else:
x = 0
c = c - 1 # You decrement c (the original length of y), ok
t = (2**c) * x
decimal = decimal + t
print (decimal)
现在可以了!
还有几点需要注意。在您非常精通阅读/编写 Python 之前,不要使用单个字符、无意义的名称。上面的解决方案读起来要好得多
binary_string = input("Enter Binario: ")
binary_string_size = len(binary_string)
decimal = 0
for character in binary_string:
if character == "1":
bit = 1
else:
bit = 0
binary_string_size -= 1
t = (2**c) * x
decimal = decimal + (2**binary_string_size) * bit
print(decimal)
稍后,当您更熟悉该语言时,请查看它执行相同的操作(甚至执行一些简单的输入验证)
binary_string = input("Enter Binario: ")
if not set(binary_string).issubset({'1', '0'}):
raise Exception("Binary string can only contain 1's and 0's")
decimal = 0
for exponent, character in enumerate(binary_string[::-1]):
if character == "1":
decimal += 2**exponent
print (decimal)
稍后,您可能还想使用列表推导
binary_string = input("Enter Binario: ")
if not set(binary_string).issubset({'1', '0'}):
raise Exception("Binary string can only contain 1's and 0's")
decimal = sum([(2**exponent) * int(char) for exponent, char in enumerate(binary_string[::-1])])