简体   繁体   English

这是我的程序,当我返回 num_list 时它不起作用但是当我打印它时它工作顺利,

[英]This is my program when i return num_list it doesn't work but when i put print it work smoothly,

This is my program when i return num_list it doesn't work but when i put print it work smoothly,my program and another friend program is exactly same but his program working and mine not.这是我的程序,当我返回 num_list 时它不起作用,但是当我打印它时,它运行顺利,我的程序和另一个朋友的程序完全相同,但他的程序工作而我的程序没有。

import random

def make_random_real():
    num_list = []
    for i in range(0, 10):
        num_list.append(random.random())
    return num_list


make_random_real()

In this version of the function, it just returns a value, so if you want to print it first you need to assign it to a variable like this:在这个版本的 function 中,它只是返回一个值,所以如果你想先打印它,你需要将它分配给这样的变量:

result = make_random_real()

then you can print(result) which will show you the num_list in your function.然后您可以print(result) ,它将显示 function 中的 num_list。

If you just want to print it without any assignment then just change return num_list to print num_list in your make_random_real() function.如果您只想在没有任何分配的情况下打印它,那么只需在您的make_random_real() function 中将return num_list更改为print num_list num_list。

Assign the return value of the function to a value, so that you can print.将 function 的返回值赋值给一个值,这样就可以打印了。

The code is fine,代码很好,

only you are not able to view output because you are not printing the returned result from the function.只有您无法查看 output,因为您没有打印 function 的返回结果。

import random

def make_random_real():
    num_list = []
    for i in range(0, 10):
        num_list.append(random.random())
    return num_list


print(make_random_real())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在 python 中放入一个函数时,我的代码不起作用 - My code doesn’t work when I put in in a function in python Python2:当我将print语句放入函数中时,递归不起作用,但否则起作用 - Python2: Recursion doesn't work when I put print statement in function but works otherwise 将HTML放入“包含”中时,为什么我的JavaScript无法正常工作? - How come my javascript doesn't work when I put my HTML in an “include”? 为什么当我在函数中进行比较时它不能正常工作? - why when I put a comparison in the function it doesn't work as it should? 当我尝试打印登录用户的 localID 时,它不起作用 - When I try to print the localID of the logged in user it doesn't work Python + Odbc:当我放置像'%'这样的通配符时,我的查询不起作用 - Python+Odbc: My query doesn't work when I put wildcards like '%' 当我运行此代码时,(打印)功能不起作用 - When I run this code, (print) function doesn't work 为什么在使用多处理模块时我的程序无法正常工作 - why my program doesn't work when I using multiprocessing module 我的(工作)树莓派程序在启动时运行不正常 - My (working) raspberry pi program doesn't work right when I run it on boot 当我使用map时,我需要将列表中的所有元素都转换为python中的int,这不起作用 - I need to convert all the elements in my list to int in python when i used map it doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM