創建一個名爲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
大,你有單元測試!現在告訴哪些測試失敗,並添加二進制轉換器的實現,這應該很容易。 –
「創建...實現...」。羅。你不能命令我去做東西。 –