鱼C论坛

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

做004讲课后作业的动动手0题,自己脑补了一大堆。。。结果自己搞不懂了

[复制链接]
发表于 2015-12-31 07:44:00 | 显示全部楼层 |阅读模式

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

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

x
代码如下:

  1. import random

  2. def typ():
  3.     while type(value) != int:
  4.         print ("不要乱说啊!!!>_<\n")
  5.         print ("请输入一个数字\n"*3)
  6.         print ("重要的事情说三遍")
  7.         value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")

  8. def butong():
  9.     if  BI <= 50:
  10.         print ("不错啊,已经很近了哦!")
  11.     elif BI <= 100:
  12.         print ("不错嘛!正确答案已经在一百以内了")
  13.     elif BI <= 1000:
  14.         print ("快了快了,误差在1000以内了哦!!!")
  15.     elif BI <= 5000:
  16.         print ("隔了将近五千呢小伙子!!!怎么搞起在?!")
  17.     elif BI > 5000:
  18.         print ("你这玩意儿也差太远了吧....")   
  19.    

  20. print ("========怪石鸟工作室荣誉出品========")

  21. res = random.randint(-10000,10000)

  22. value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")

  23. typ()

  24. value = int(value)

  25. if value == res:
  26.     print ("哎呀妈呀...怎么第一遍就被你猜对了>_>这是神马狗屎运啊!!")
  27. else:
  28.     print ("嘿!-_-猜错了吧!")
  29.     end = 19
  30.     for it in range(19):
  31.         value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
  32.         typ()
  33.         value = int(value)
  34.         if value == res:
  35.             print ("哎呀妈呀...怎么就被猜中了呢...>_>这是神马狗屎运啊!!")
  36.             end  =  None
  37.             break
  38.         BI = abs( value - res )
  39.         if value < res:
  40.             butong()
  41.             print("小了小了!")
  42.             end -= 1
  43.         else:
  44.             butong()
  45.             print("大了大了!")
  46.             end -= 1
  47.     if end == 0:
  48.         print("都猜这么多次了!不跟你玩啦...哼!!!!")

  49. print("游戏结束,不跟你完啦<_>")


复制代码




这个是报的异常:

========怪石鸟工作室荣誉出品========
嘿!小伙子!猜猜怪石鸟心里在想什么数字? :50
Traceback (most recent call last):
  File "T:/Christophe/dossier_de_cours/python/ShuZiYouXi.py", line 27, in <module>
    typ()
  File "T:/Christophe/dossier_de_cours/python/ShuZiYouXi.py", line 4, in typ
    while type(value) != int:
UnboundLocalError: local variable 'value' referenced before assignment

刚开始写的在定义函数typ的时候写的是while type(value) != int:在检查以后我发现是死循环,因为input的是一个字符串,原始我把那一行修改成了这个:while not value.isnumeric():

但是接下来程序继续报错:
========怪石鸟工作室荣誉出品========
嘿!小伙子!猜猜怪石鸟心里在想什么数字? :50
Traceback (most recent call last):
  File "T:/Christophe/dossier_de_cours/python/ShuZiYouXi.py", line 27, in <module>
    typ()
  File "T:/Christophe/dossier_de_cours/python/ShuZiYouXi.py", line 4, in typ
    while not value.isnumeric():
UnboundLocalError: local variable 'value' referenced before assignment

然后把英文拿到Google翻译上一看,是局部变量的问题,于是乎我就在定义typ函数的while那句下面加了一句"global value"

所以,最后代码变成这样:

  1. import random

  2. def typ():
  3.     while not value.isnumeric():
  4.         print ("不要乱说啊!!!>_<\n")
  5.         print ("请输入一个数字\n"*3)
  6.         print ("重要的事情说三遍")
  7.         global value
  8.         value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")

  9. def butong():
  10.     if  BI <= 50:
  11.         print ("不错啊,已经很近了哦!")
  12.     elif BI <= 100:
  13.         print ("不错嘛!正确答案已经在一百以内了")
  14.     elif BI <= 1000:
  15.         print ("快了快了,误差在1000以内了哦!!!")
  16.     elif BI <= 5000:
  17.         print ("隔了将近五千呢小伙子!!!怎么搞起在?!")
  18.     elif BI > 5000:
  19.         print ("你这玩意儿也差太远了吧....")   

  20. print ("========怪石鸟工作室荣誉出品========")

  21. res = random.randint(-10000,10000)

  22. value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")

  23. typ()

  24. value = int(value)

  25. if value == res:
  26.     print ("哎呀妈呀...怎么第一遍就被你猜对了>_>这是神马狗屎运啊!!")
  27. else:
  28.     print ("嘿!-_-猜错了吧!")
  29.     end = 19
  30.     for it in range(19):
  31.         value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
  32.         typ()
  33.         value = int(value)
  34.         if value == res:
  35.             print ("哎呀妈呀...怎么就被猜中了呢...>_>这是神马狗屎运啊!!")
  36.             end  =  None
  37.             break
  38.         BI = abs( value - res )
  39.         if value < res:
  40.             butong()
  41.             print("小了小了!")
  42.             end -= 1
  43.         else:
  44.             butong()
  45.             print("大了大了!")
  46.             end -= 1
  47.     if end == 0:
  48.         print("都猜这么多次了!不跟你玩啦...哼!!!!")

  49. print("游戏结束,不跟你完啦<_>")



复制代码


然后我自己试了几次,然后悲剧的发现我的typ函数无法把负号"-"当作数字字符,于是在负数的情况下就会返回一个False。。。然后我就只好再多加上几次判断。。。。判断value的第一个字符是正好还是负号,然后一个一个字符抽出来判断是否是数字字符。。。

  1. import random

  2. def typ():
  3.     if value[0] == '-' or '+':
  4.         for it in range(1,len(value)):
  5.             if not value[it].isnumeric():
  6.                 print ("不要乱说啊!!!>_<\n")
  7.                 print ("请输入一个数字\n"*3)
  8.                 print ("重要的事情说三遍")
  9.                 global value
  10.                 value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")
  11.                 return
  12.     else:
  13.         print ("不要乱说啊!!!>_<\n")
  14.         print ("请输入一个数字\n"*3)
  15.         print ("重要的事情说三遍")
  16.         global value
  17.         value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")


  18. def butong():
  19.     if  BI <= 50:
  20.         print ("不错啊,已经很近了哦!")
  21.     elif BI <= 100:
  22.         print ("不错嘛!正确答案已经在一百以内了")
  23.     elif BI <= 1000:
  24.         print ("快了快了,误差在1000以内了哦!!!")
  25.     elif BI <= 5000:
  26.         print ("隔了将近五千呢小伙子!!!怎么搞起在?!")
  27.     elif BI > 5000:
  28.         print ("你这玩意儿也差太远了吧....")
  29.    

  30. print ("========怪石鸟工作室荣誉出品========")

  31. res = random.randint(-10000,10000)

  32. value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")

  33. typ()

  34. value = int(value)

  35. if value == res:
  36.     print ("哎呀妈呀...怎么第一遍就被你猜对了>_>这是神马狗屎运啊!!")
  37. else:
  38.     print ("嘿!-_-猜错了吧!")
  39.     end = 19
  40.     for it in range(19):
  41.         value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
  42.         typ()
  43.         value = int(value)
  44.         if value == res:
  45.             print ("哎呀妈呀...怎么就被猜中了呢...>_>这是神马狗屎运啊!!")
  46.             end  =  None
  47.             break
  48.         BI = abs( value - res )
  49.         if value < res:
  50.             butong()
  51.             print("小了小了!")
  52.             end -= 1
  53.         else:
  54.             butong()
  55.             print("大了大了!")
  56.             end -= 1
  57.     if end == 0:
  58.         print("都猜这么多次了!不跟你玩啦...哼!!!!")

  59. print("游戏结束,不跟你完啦<_>")



复制代码


然后写好代码。。。自己测试发现没有问题如图 QQ截图20151231003311.png



然后本来一切都结束了。。。我发现我自己搞不懂了,我的代码应该还存在问题的啊。当我输入正数的时候如果不带+加号的话他的typ函数就会判断我输入了错误的数据了啊。
比如“5000”我的typ先判断是否以正负号开头,不是,然后就执行下面else了啊?

有点没懂,然后我自己猜测是不是字符串里是正数的话会默认带一个+加号,然后如图,自己测试发现不是,

QQ截图20151231004354.png

请问哪位能解释一下为什么我的代码在输入一个不带正号的正数时不进入else,而是直接pass掉了?


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-31 14:52:47 | 显示全部楼层
现有的字符串方法都不好判断,建议采用异常处理:
  1. def typ():
  2.     while True:
  3.         print ("不要乱说啊!!!>_<\n")
  4.         print ("请输入一个数字\n"*3)
  5.         print ("重要的事情说三遍")
  6.         temp = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")
  7.         try:
  8.             value = int(temp)
  9.             return value
  10.         except ValueError:
  11.             pass
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-31 22:18:10 | 显示全部楼层
冬雪雪冬 发表于 2015-12-31 14:52
现有的字符串方法都不好判断,建议采用异常处理:

不不不不不不,我最后问题的意思是那个为什么我的程序正确运行了呢?

按最后的版本在没有输入加号的时候他应该运行不了啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-1 14:19:41 | 显示全部楼层
漆雕古代 发表于 2015-12-31 22:18
不不不不不不,我最后问题的意思是那个为什么我的程序正确运行了呢?

按最后的版本在没有输入加号的时 ...

把typ函数中的判断改为if value[0] == '-' or value[0] == '+':
否则永远为True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-20 14:29:53 | 显示全部楼层
冬雪雪冬 发表于 2016-1-1 14:19
把typ函数中的判断改为if value[0] == '-' or value[0] == '+':
否则永远为True

原来如此,我有点明白了
谢谢啦!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-19 01:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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