创建一个名为binary_converter的函数。在函数内部,实现一个算法,将0到255之间的十进制数转换为它们的二进制等值。二进制转换器alogrithim
对于任何无效的输入,返回字符串无效输入
示例:5号返回的字符串101
我的代码
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')
代码有错误请我是新来的节目,家庭实际学习
编辑 代码
def binary_converter(n):
if(n==0):
return "0"
elif(n>255):
print("out of range")
return ""
else:
ans=""
while(n>0):
temp=n%2
ans=str(temp)+ans
n=n/2
return ans
错误报告
有在你的代码
Results: {"finished": true, "success": [{"fullName": "test_conversion_one", "passedSpecNumber": 1}, {"fullName": "test_conversion_two", "passedSpecNumber": 2}], "passed": false, "started": true, "failures": [{"failedSpecNumber": 1, "fullName": "test_no_negative_numbers", "failedExpectations": [{"message": "Failure in line 19, in test_no_negative_numbers\n
self.assertEqual(result, 'Invalid input', msg='Input below 0 not allowed')\nAssertionError: Input below 0 not allowed\n"}]}, {"failedSpecNumber": 2, "fullName": "test_no_numbers_above_255", "failedExpectations": [{"message": "Failure in line 23, in test_no_numbers_above_255\n self.assertEqual(result, 'Invalid input', msg='Input above 255 not allowed')\nAssertionError: Input above 255 not allowed\n"}]}], "specs": {"count": 4, "pendingCount": 0, "time": "0.000112"}} out of range
我想你会需要向我们展示了'binary_converter'代码 - 以及你的代码实际上是产生任何错误... – mgilson
大,你有单元测试!现在告诉哪些测试失败,并添加二进制转换器的实现,这应该很容易。 –
“创建...实现...”。罗。你不能命令我去做东西。 –