【问题标题】:Python | I need help for my class (Changing from binary to decimal)蟒蛇 |我的班级需要帮助(从二进制更改为十进制)
【发布时间】:2017-10-02 03:30:49
【问题描述】:

我需要帮助将课程从二进制更改为十进制,如果有人可以帮助我解决我可能出错的问题,这就是我所拥有的,(注意这是我必须将其上交的格式,没有其他功能)我有什么问题?

x = str (input("Enter Binario: "))
c = len (x)
decimal = 0
b ="" 
for y in (b):
    if ( b == "1"):
        b = 1
    else :
        if (b =="0"):
            b =0
    z = c - 1
    t = (2**z) * x
    decimal = decimal + t 
print (decimal) 

【问题讨论】:

  • 一方面,您将b 设置为"",然后尝试对其进行迭代。由于它是空的,循环将立即退出。也许迭代 x 而不是 b 更有意义。
  • 在循环内部,您正在查看b。您想查看y(一旦将其更改为迭代x)。
  • 在循环内部,您将b 设置为01,然后将其丢弃。为什么不为它选择一个新的变量名,然后用它来构造你的结果?

标签: python binary decimal


【解决方案1】:

我了解您只是在学习,但仔细查看您的代码,您应该会发现很多简单的错误。

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

【讨论】:

    猜你喜欢
    • 2011-04-03
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2011-06-20
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多