def int_To_reverese_binary(integer_value): bits = []
bits.append(str(0 if integer_value%2 == 0 else1)) while integer_value > 1: integer_value = integer_value //2 bits.append(str(0 if integer_value%2 ==0 else 1))
return ''.join(bits)
def string_reverese(input_string): str = "" for i in input_string: str = i + str return str
if __name__ == '__main__':
# decimal value val = int(input())
# Calling function
print(string_reverese(int_To_reverese_binary(val)))
The code I've provided above seems to provide a correct binaryinteger for the conversion when ran across several numbers, howeverwhen i go to submit my code, i run into this error, which ido not understand so i can't fix it. (There are several examples ofthis problem on answers, however each one runs into an error at somepoint. Several of the examples place a 6 in the function when theyprint the answer, however it has to work with several values. whenremoving the 6 and inserting the appropiate variable, i ran into avariable not defined error.) This is homework from zybooks so it isunusually picky. Thank you.
6.21 LAB: Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and O's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 6 Output x x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string. Ex: If the input is: the output is: 110 2 (remainder is either 0 or 1) The program must define and call the following two functions. Define a function named int_to_reverse_binary() that takes an integer as a parameter and returns a string of 1's and O's representing the integer in binary (in reverse). Define a function named string_reverse() that takes an input string as a parameter and returns a string representing the input string in reverse. def int_to_reverse_binary(integer_value) def string_reverse (input_string)
Latest submission - 2:55 PM CDT on 07/02/22 Only show failing tests 1: Compare output 2: Unit test 1 Input 6 4: Unit test Your output 110 Total score: 2/10 < Download this submission Convert 19 to binary using int_to_reverse_binary() and string_reverse() Check variables and functions in submission are named correctly cannot import name 'int_to_reverse_binary' from 'main' (/home/runner/local/submission/uni 2/2 0/2 3: Unit test A Convert 255 to binary using int_to_reverse_binary() and string_reverse() Check variables and functions in submission are named correctly cannot import name 'int_to_reverse_binary' from 'main' (/home/runner/local/submission/uni 0/3 0/3 Convert 122 to binary using int_to_reverse_binary() and string_reverse() in submission are named correctly everse_binary' from 'main' (/home/runner/local/submission/unit_test_student_code/main.py)
def int_To_reverese_binary(integer_value): bits = [] bits.append(str(0 if integer_value%2 == 0 else 1)) while in
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am