鱼C论坛

 找回密码
 立即注册
查看: 920|回复: 7

[已解决]random()和isdigit()第四五讲中出现的函数,怎么知道函数的功能,以及有什么函数可...

[复制链接]
发表于 2020-5-1 20:03:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如题random()和isdigit()第四五讲中出现的函数,怎么知道函数的功能,以及有什么函数可以用?
使用了dir(__builtins__)发现列表里没有这两个函数,又用了help(isdigit)提示语法错误,isdigit未定义名称。
本人编程小白,刚刚开始学python,望各位大神指点!
最佳答案
2020-5-1 21:05:33
你应该是说 randint() 和 isdigit() 吧。首先 isdigit() 是字符串的方法,它使用前必须加上 str. ,str 代表字符串对象,英文点号表示 isdigit() 方法是属于字符串的,所以应该:

  1. >>> help(str.isdigit)
  2. Help on method_descriptor:

  3. isdigit(self, /)
  4.     Return True if the string is a digit string, False otherwise.
  5.    
  6.     A string is a digit string if all characters in the string are digits and there
  7.     is at least one character in the string.
复制代码


randint() 是属于 random 模块的函数,需要先从 random 模块中导入:

  1. >>> from random import randint
  2. >>> help(randint)
  3. Help on method randint in module random:

  4. randint(a, b) method of random.Random instance
  5.     Return random integer in range [a, b], including both end points.
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-1 20:07:47 | 显示全部楼层
random在random模块,要先导入
  1. from random import random
复制代码
isdigit是str类的函数
  1. help(str.isdigit)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-1 20:17:32 | 显示全部楼层
本帖最后由 Twilight6 于 2020-5-1 20:21 编辑

呃。。。我觉得如果你英语一般般或者很棒,才用help看的懂里面的文档
不然像我。。。help之后只看得懂参数部分,而文档部分。。。咳咳
还是百度更适合我这种英语学渣,唉,要好好学英语了!!!

不过你可以去查查官方文档吧~英语好直接看英语的

这边附上中文的:https://docs.python.org/zh-cn/3.7/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-1 20:46:59 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-5-1 20:07
random在random模块,要先导入isdigit是str类的函数

不是import random

from random import random 是什么东西?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-1 20:52:38 | 显示全部楼层
MIke_python小小 发表于 2020-5-1 20:46
不是import random

from random import random 是什么东西?

可以自己试试,random是一个模块,他里面有个叫random的函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-1 21:05:33 | 显示全部楼层    本楼为最佳答案   
你应该是说 randint() 和 isdigit() 吧。首先 isdigit() 是字符串的方法,它使用前必须加上 str. ,str 代表字符串对象,英文点号表示 isdigit() 方法是属于字符串的,所以应该:

  1. >>> help(str.isdigit)
  2. Help on method_descriptor:

  3. isdigit(self, /)
  4.     Return True if the string is a digit string, False otherwise.
  5.    
  6.     A string is a digit string if all characters in the string are digits and there
  7.     is at least one character in the string.
复制代码


randint() 是属于 random 模块的函数,需要先从 random 模块中导入:

  1. >>> from random import randint
  2. >>> help(randint)
  3. Help on method randint in module random:

  4. randint(a, b) method of random.Random instance
  5.     Return random integer in range [a, b], including both end points.
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-5-1 21:48:44 | 显示全部楼层
MIke_python小小 发表于 2020-5-1 20:46
不是import random

from random import random 是什么东西?

random模块确实有个函数叫random
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-9 16:38:50 | 显示全部楼层
zltzlt 发表于 2020-5-1 21:05
你应该是说 randint() 和 isdigit() 吧。首先 isdigit() 是字符串的方法,它使用前必须加上 str. ,str 代 ...

非常感谢你的解答,解释的非常清楚。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 08:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表