Python |我需要帮助我的班级(从二进制改为十进制)

时间:2017-10-02 03:30:50

标签: python binary decimal

我需要帮助从二进制更改为十进制为类,如果有人可以帮我修复我可能有错的东西,这就是我所拥有的(请注意,这是我必须将其转入的格式,没有其他功能)我有什么不对?

/simplified

1 个答案:

答案 0 :(得分:2)

我知道你只是在学习,但仔细看看你的代码,你会发现很多简单的错误。

override func viewDidLoad() {
    super.viewDidLoad()
    let toolBar = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 50))
    toolBar.backgroundColor = .gray
    let doneButton = UIButton(type: .custom)
    doneButton.setTitle("Done", for: .normal)
    doneButton.translatesAutoresizingMaskIntoConstraints = false
    toolBar.addSubview(doneButton)
    toolBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[button]-|", 
                                                          options: .directionLeadingToTrailing, 
                                                          metrics: nil, 
                                                          views: ["button":doneButton]))
    toolBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[button]|", 
                                                          options: .directionLeadingToTrailing, 
                                                          metrics: nil, 
                                                          views: ["button":doneButton]))
    self.emailTextField.inputAccessoryView = toolBar 
}

所以让我们解决第一个问题。

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)

现在有效!

还有一些事项需要注意。在你熟练阅读/编写Python之前,不要使用单个字符,毫无意义的名字。

以上解决方案读得更好
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)

稍后,当您对该语言更加熟悉时,请注意这是做同样的事情(甚至做一些简单的输入验证)

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)