鱼C论坛

 找回密码
 立即注册
查看: 1639|回复: 10

[已解决]为什么会出现报错

[复制链接]
发表于 2017-9-29 13:23:21 | 显示全部楼层 |阅读模式

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

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

x
  1. #!/usr/bin/python
  2. #-*- coding:utf-8 -*-


  3. import random, dropdata



  4. def test_drop(drop_id,drop_num):
  5.     list1 = []
  6.     box = [i['itemid'] for i in dropdata.data[drop_id]]
  7.     weight = [i['weight'] for i in dropdata.data[drop_id]]
  8.     for i in range(drop_num):
  9.         r = random.randint(1, sum(weight))
  10.         if r <= weight[0]:
  11.             list1.append(box[0])
  12.         elif r <= sum(weight[:2]):
  13.             list1.append(box[1])
  14.         elif r <= sum(weight[:3]):
  15.             list1.append(box[2])
  16.         elif r <= sum(weight[:4]):
  17.             list1.append(box[3])
  18.         else:
  19.             list1.append(box[4])
  20.     return list1
  21.                      
  22.             
  23.         
  24.    
  25. if __name__ == '__main__':
  26.     while True:
  27.         x = int(input('请输入宝箱种类:1金宝箱,2银宝箱,3铜宝箱,0退出:'))
  28.         if x not in (0, 1, 2, 3):
  29.             print "输入错误,重新输入"
  30.         if x == 0:
  31.             break
  32.         print(test_drop(x, 1))
  33. 为什么输入除了0.1.2.3时的其他东西是会出现这样报错
  34. [code]请输入宝箱种类:1金宝箱,2银宝箱,3铜宝箱,0退出:4
  35. 输入错误,重新输入

  36. Traceback (most recent call last):
  37.   File "E:\python文件夹\drop.py", line 37, in <module>
  38.     print(test_drop(x, 1))
  39.   File "E:\python文件夹\drop.py", line 11, in test_drop
  40.     box = [i['itemid'] for i in dropdata.data[drop_id]]
  41. KeyError: 4
复制代码
最佳答案
2017-9-29 14:27:53
sw546190767 发表于 2017-9-29 14:06
那如果要输入字母这些东西也出现报错提醒应该怎么弄

在32行先不要int(),首先判断input返回的字符串是否为数字(用.isdigit),然后在int()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-29 13:38:53 | 显示全部楼层
在34行print "输入错误,重新输入"后加一行continue,缩进与34行相同
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-29 14:06:02 | 显示全部楼层
冬雪雪冬 发表于 2017-9-29 13:38
在34行print "输入错误,重新输入"后加一行continue,缩进与34行相同

那如果要输入字母这些东西也出现报错提醒应该怎么弄
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 14:14:43 | 显示全部楼层
  1. import random, dropdata



  2. def test_drop(drop_id,drop_num):
  3.     list1 = []
  4.     box = [i['itemid'] for i in dropdata.data[drop_id]]
  5.     weight = [i['weight'] for i in dropdata.data[drop_id]]
  6.     for i in range(drop_num):
  7.         r = random.randint(1, sum(weight))
  8.         if r <= weight[0]:
  9.             list1.append(box[0])
  10.         elif r <= sum(weight[:2]):
  11.             list1.append(box[1])
  12.         elif r <= sum(weight[:3]):
  13.             list1.append(box[2])
  14.         elif r <= sum(weight[:4]):
  15.             list1.append(box[3])
  16.         else:
  17.             list1.append(box[4])
  18.     return list1
  19.                      
  20.             
  21.         
  22.    
  23. if __name__ == '__main__':
  24.     while True:
  25.         x = input('请输入宝箱种类:1金宝箱,2银宝箱,3铜宝箱,0退出:')
  26.         
  27.         if x.isdigit():
  28.             x = int(x)
  29.             if x in (0, 1, 2, 3):
  30.                 if x == 0:
  31.                     break            
  32.                 print(test_drop(x, 1))
  33.             else:
  34.                 print("输入错误,重新输入")
  35.         else:
  36.             print("输入错误,重新输入")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 14:27:53 | 显示全部楼层    本楼为最佳答案   
sw546190767 发表于 2017-9-29 14:06
那如果要输入字母这些东西也出现报错提醒应该怎么弄

在32行先不要int(),首先判断input返回的字符串是否为数字(用.isdigit),然后在int()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-29 16:55:11 | 显示全部楼层
冬雪雪冬 发表于 2017-9-29 14:27
在32行先不要int(),首先判断input返回的字符串是否为数字(用.isdigit),然后在int()

不是很明白
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 17:07:33 | 显示全部楼层
  1. while True:
  2.     x = input('请输入数字0~4')
  3.     if x.isdigit():
  4.         x = int(x)
  5.         if x<0 or x > 4:
  6.             print('输入0~4')
  7.         else:
  8.             break
  9.     else:
  10.         print('输入数字')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-29 17:43:32 | 显示全部楼层

提示报错
  1. Traceback (most recent call last):
  2.   File "E:\python文件夹\drop.py", line 33, in <module>
  3.     if x.isdigit():
  4. AttributeError: 'int' object has no attribute 'isdigit'
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 17:44:52 From FishC Mobile | 显示全部楼层
转成整数后就不能有用isdigit了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-29 18:16:57 | 显示全部楼层
冬雪雪冬 发表于 2017-9-29 17:44
转成整数后就不能有用isdigit了

那输入要1.2.3怎么弄
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 18:48:36 From FishC Mobile | 显示全部楼层
按8楼的程序输入0~4就退出循环进入下面语句
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-4 03:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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