にアルゴリズムを使用してバイナリに小数を変換するために、何がこのコードが間違っているしてください方法のpython
def binary_converter(n):
n = int(n)
if n == 0:
return '0'
elif n not in (0,255):
return 'Invalid input'
elif n in range (0,255):
return binary_converter(n//2) + str(n%2)
else:
return 'Invalid conversion'## Heading ##
here is the test
import unittest
class BinaryConverterTestCases(unittest.TestCase):
def test_conversion_one(self):
result = binary_converter(0)
self.assertEqual(result, '0', msg='Invalid conversion')
def test_conversion_two(self):
result = binary_converter(62)
self.assertEqual(result, '111110', msg='Invalid conversion')
def test_no_negative_numbers(self):
result = binary_converter(-1)
self.assertEqual(result, 'Invalid input', msg='Input below 0 not allowed')
def test_no_numbers_above_255(self):
result = binary_converter(300)
self.assertEqual(result, 'Invalid input', msg='Input above 255 not allowed')
理由は特殊なケースですか? –
@DarthKotikテストでは無効な入力を返すことになっています。 – David
@PadraicCunninghamあなたの正しいが、私はまだそれを提出するときにエラーが発生します.P/Sそれはandela家庭研究のための割り当てです..私は本当にこれを正しく学ぶためには完全なコードではなく、アドバイスが必要です。ありがとうすべて – David