Home > database >  Unable to properly return python data in function
Unable to properly return python data in function

Time:04-05

I've been at this for hours and hours. I think my problem is I need to use a return in my first function so that I can use this function as an argument in my second function. However, it seems that if I use a return, the data is somehow not being passed properly to the second function. I say that because I can't seem to format it properly if i comment out my print statement and only use a return (the return statement won't let me include the end = '' so it comes out vertically instead). Then the second function just spits out the first digit of my first function's return. I'm so lost and i need to get some sleep now I guess. Been up all night with this. Is there some way I can return the data int he first function and make it be a nice horizontal string like it would be if I used my print statement instead? (Or does that not matter and I'm way off track?) Please let me know if I can clarify something. Just a nudge in the right direction would help.

Instructions: Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:

As long as x is greater than 0
Output x % 2 (remainder is either 0 or 1)
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:6

the output is: 110

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 0'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)

My code:

Define your functions here.

def int_to_reverse_binary(int_number):
    while int_number > 0:
       #print (int_number % 2, end='')
       return int_number % 2
       int_number = int_number // 2
       

def string_reverse(input_string):
    for i in reversed(str(input_string)):
        print(i,end='')
        
    
    
    
if __name__ == '__main__':
    # Type your code here. 
    # Your code must call int_to_reverse_binary() to get 
    # the binary string of an integer in a reverse order.
    # Then call string_reverse() to reverse the string
    # returned from int_to_reverse_binary().
    x = int(input())
    int_to_reverse_binary(x)
    string_reverse(int_to_reverse_binary(x))

1: Compare output 0 / 2 Output differs. See highlights below. Input 6 Your output 0 Expected output 110

2: Unit test 0 / 2 Convert 19 to binary using int_to_reverse_binary() and string_reverse() Your output 1 Test feedback string_reverse(user_input) did not return a value. Your function may be missing a return statement.

3: Unit test 0 / 3 Convert 255 to binary using int_to_reverse_binary() and string_reverse() Your output 1 Test feedback string_reverse(user_input) did not return a value. Your function may be missing a return statement.

CodePudding user response:

You seem to have made this unnecessarily complex. f-string formatting will give you the binary representation of your integer then reverse the string with a slice as follows:

def int_to_reverse_binary(int_number):
    return f'{int_number:b}'[::-1]

print(int_to_reverse_binary(100))

Output:

0010011

CodePudding user response:

The return statement in Python also acts as the ending point of the function. i.e. no statement will be executed once a return statement is encountered in a function. So, when the while loop is being executed, the interpreter sees a return statement and stops executing any further. If you wish to return multiple values from the function you can do 2 things,

  1. Instead of using a while loop in s function, use the function in the while loop: Sample Code:
def foo(num):
    return num % 2

i = 0
while i< 10:
    print(foo(i))
    i  = 1
  1. Use a list to return all values at once. Sample Code:
def foo(num):
    a = []
    i = 0
    while i < num:
        a.append(i)
        i =1
print(foo(10))

Code With corrections:

def int_to_reverse_binary(int_number):
    # print('i', int_number)
    a = []
    while int_number > 0:
        a.append(int_number % 2)
        int_number = int_number // 2
    # print('a', a)
    return a
       
def string_reverse(input_string):
    print(''.join([str(i) for i in input_string])[::-1])
        
        
if __name__ == '__main__':
    x = int(input())
    # a = int_to_reverse_binary(x)
    string_reverse(int_to_reverse_binary(x))
  • Related