鱼C论坛

 找回密码
 立即注册
查看: 1647|回复: 4

[已解决]为什么这个代码老是运行报错呢?我是跟着老师一起写的,python 3.8.2版本

[复制链接]
发表于 2021-8-8 10:11:14 | 显示全部楼层 |阅读模式

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

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

x
  1. count=1
  2. while count<=10:
  3.     person=int(input('请出拳:[0:石头  1:剪刀  2:布]'))
  4.     computer=random.randint(0,2)
  5.     if person==0 and computer==1:  #多条件
  6.         print('厉害了,你赢了')
  7.         pass
  8.     elif person==1 and computer==2:
  9.         print('厉害了,你赢了')
  10.         pass
  11.     elif person==2 and computer==0:
  12.         print('厉害了,你赢了')
  13.         pass
  14.     elif person==computer:
  15.         print('不错,居然是平手')
  16.         pass

  17.     else:
  18.         print('哈哈  我输了')
  19.         pass
  20.     count+=1
复制代码



总是报错,为什么?


请出拳:[0:石头  1:剪刀  2:布]1
Traceback (most recent call last):
  File "E:\python\while.py", line 21, in <module>
    computer=random.randint(0,2)
NameError: name 'random' is not defined
最佳答案
2021-8-8 10:25:47
需要导入random这个库
  1. import random
  2. count=1
  3. while count<=10:
  4.     person=int(input('请出拳:[0:石头  1:剪刀  2:布]'))
  5.     computer=random.randint(0,2)
  6.     if person==0 and computer==1:  #多条件
  7.         print('厉害了,你赢了')
  8.         pass
  9.     elif person==1 and computer==2:
  10.         print('厉害了,你赢了')
  11.         pass
  12.     elif person==2 and computer==0:
  13.         print('厉害了,你赢了')
  14.         pass
  15.     elif person==computer:
  16.         print('不错,居然是平手')
  17.         pass

  18.     else:
  19.         print('哈哈  我输了')
  20.         pass
  21.     count+=1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 10:25:00 | 显示全部楼层
第一行加
import random
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 10:25:47 | 显示全部楼层    本楼为最佳答案   
需要导入random这个库
  1. import random
  2. count=1
  3. while count<=10:
  4.     person=int(input('请出拳:[0:石头  1:剪刀  2:布]'))
  5.     computer=random.randint(0,2)
  6.     if person==0 and computer==1:  #多条件
  7.         print('厉害了,你赢了')
  8.         pass
  9.     elif person==1 and computer==2:
  10.         print('厉害了,你赢了')
  11.         pass
  12.     elif person==2 and computer==0:
  13.         print('厉害了,你赢了')
  14.         pass
  15.     elif person==computer:
  16.         print('不错,居然是平手')
  17.         pass

  18.     else:
  19.         print('哈哈  我输了')
  20.         pass
  21.     count+=1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-8 10:32:35 | 显示全部楼层
  1. person=int(input('请出拳:[0:石头  1:剪刀  2:布]'))
  2. computer=random.randint(0,2)
  3. if person==0 and computer==1:  #多条件
  4.     print('厉害了,你赢了')
  5.     pass
  6. elif person==1 and computer==2:
  7.     print('厉害了,你赢了')
  8.     pass
  9. elif person==2 and computer==0:
  10.     print('厉害了,你赢了')
  11.     pass
  12. elif person==computer:
  13.     print('不错,居然是平手')
  14.     pass

  15. else:
  16.     print('哈哈  我输了')
复制代码


这个代码没有加import random就可以运行,是因为random在if语句外就已经定义了是吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 11:29:44 | 显示全部楼层
czzhbq 发表于 2021-8-8 10:32
这个代码没有加import random就可以运行,是因为random在if语句外就已经定义了是吗?

这个也会报错的,必须要导入random这个模块,才能使用
如果问题已解决,请及时设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-27 09:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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