无法在函数中正确 return python 数据

Unable to properly return python data in function

我已经为此忙了几个小时了。我想我的问题是我需要在我的第一个函数中使用 return ,这样我就可以在我的第二个函数中使用这个函数作为参数。但是,如果我使用 return,数据似乎不会以某种方式正确传递给第二个函数。我这么说是因为如果我注释掉我的 print 语句并且只使用 return (return 语句不允许我包含 end = '' 所以它似乎无法正确格式化而是垂直出来)。然后第二个函数只是吐出我第一个函数的 return 的第一个数字。我迷路了,我想我现在需要睡会儿觉。为此整夜未眠。有什么方法可以 return 数据 int 他第一个函数并使它成为一个很好的水平字符串,就像我使用 print 语句代替一样? (或者这无关紧要,我偏离了轨道?)如果我能澄清一些事情,请告诉我。只要向正确的方向推动就会有所帮助。

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)

我的代码:

在此处定义您的函数。

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:比较输出 0 / 2 输出不同。请参阅下面的要点。 输入 6个 你的输出 0 预期产出 110

2:单元测试 0 / 2 使用 int_to_reverse_binary() 和 string_reverse() 将 19 转换为二进制 你的输出 1个 测试反馈 string_reverse(user_input) 没有 return 值。 您的函数可能缺少 return 语句。

3:单元测试 0 / 3 使用 int_to_reverse_binary() 和 string_reverse() 将 255 转换为二进制 你的输出 1个 测试反馈 string_reverse(user_input) 没有 return 值。 您的函数可能缺少 return 语句。

Python中的return语句也作为函数的结束点。即,一旦在函数中遇到 return 语句,将不会执行任何语句。因此,当执行 while 循环时,解释器会看到 return 语句并停止进一步执行。如果你想从函数中 return 多个值,你可以做两件事,

  1. 不要在 s 函数中使用 while 循环,而是在 while 循环中使用函数: 示例代码:
def foo(num):
    return num % 2

i = 0
while i< 10:
    print(foo(i))
    i += 1
  1. 一次使用 list 到 return 所有值。示例代码:
def foo(num):
    a = []
    i = 0
    while i < num:
        a.append(i)
        i+=1
print(foo(10))

更正后的代码:

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

你似乎把这个复杂化了。 f-string 格式化将为您提供整数的二进制表示,然后用切片反转字符串,如下所示:

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

print(int_to_reverse_binary(100))

输出:

0010011