|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
- import random
- def typ():
- while type(value) != int:
- print ("不要乱说啊!!!>_<\n")
- print ("请输入一个数字\n"*3)
- print ("重要的事情说三遍")
- value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")
- def butong():
- if BI <= 50:
- print ("不错啊,已经很近了哦!")
- elif BI <= 100:
- print ("不错嘛!正确答案已经在一百以内了")
- elif BI <= 1000:
- print ("快了快了,误差在1000以内了哦!!!")
- elif BI <= 5000:
- print ("隔了将近五千呢小伙子!!!怎么搞起在?!")
- elif BI > 5000:
- print ("你这玩意儿也差太远了吧....")
-
- print ("========怪石鸟工作室荣誉出品========")
- res = random.randint(-10000,10000)
- value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
- typ()
- value = int(value)
- if value == res:
- print ("哎呀妈呀...怎么第一遍就被你猜对了>_>这是神马狗屎运啊!!")
- else:
- print ("嘿!-_-猜错了吧!")
- end = 19
- for it in range(19):
- value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
- typ()
- value = int(value)
- if value == res:
- print ("哎呀妈呀...怎么就被猜中了呢...>_>这是神马狗屎运啊!!")
- end = None
- break
- BI = abs( value - res )
- if value < res:
- butong()
- print("小了小了!")
- end -= 1
- else:
- butong()
- print("大了大了!")
- end -= 1
- if end == 0:
- print("都猜这么多次了!不跟你玩啦...哼!!!!")
- 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"
所以,最后代码变成这样:
- import random
- def typ():
- while not value.isnumeric():
- print ("不要乱说啊!!!>_<\n")
- print ("请输入一个数字\n"*3)
- print ("重要的事情说三遍")
- global value
- value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")
- def butong():
- if BI <= 50:
- print ("不错啊,已经很近了哦!")
- elif BI <= 100:
- print ("不错嘛!正确答案已经在一百以内了")
- elif BI <= 1000:
- print ("快了快了,误差在1000以内了哦!!!")
- elif BI <= 5000:
- print ("隔了将近五千呢小伙子!!!怎么搞起在?!")
- elif BI > 5000:
- print ("你这玩意儿也差太远了吧....")
- print ("========怪石鸟工作室荣誉出品========")
- res = random.randint(-10000,10000)
- value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
- typ()
- value = int(value)
- if value == res:
- print ("哎呀妈呀...怎么第一遍就被你猜对了>_>这是神马狗屎运啊!!")
- else:
- print ("嘿!-_-猜错了吧!")
- end = 19
- for it in range(19):
- value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
- typ()
- value = int(value)
- if value == res:
- print ("哎呀妈呀...怎么就被猜中了呢...>_>这是神马狗屎运啊!!")
- end = None
- break
- BI = abs( value - res )
- if value < res:
- butong()
- print("小了小了!")
- end -= 1
- else:
- butong()
- print("大了大了!")
- end -= 1
- if end == 0:
- print("都猜这么多次了!不跟你玩啦...哼!!!!")
- print("游戏结束,不跟你完啦<_>")
复制代码
然后我自己试了几次,然后悲剧的发现我的typ函数无法把负号"-"当作数字字符,于是在负数的情况下就会返回一个False。。。然后我就只好再多加上几次判断。。。。判断value的第一个字符是正好还是负号,然后一个一个字符抽出来判断是否是数字字符。。。
- import random
- def typ():
- if value[0] == '-' or '+':
- for it in range(1,len(value)):
- if not value[it].isnumeric():
- print ("不要乱说啊!!!>_<\n")
- print ("请输入一个数字\n"*3)
- print ("重要的事情说三遍")
- global value
- value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")
- return
- else:
- print ("不要乱说啊!!!>_<\n")
- print ("请输入一个数字\n"*3)
- print ("重要的事情说三遍")
- global value
- value = input ("不要再搞错了哦=_=,怪石鸟心里在想什么数字! :")
- def butong():
- if BI <= 50:
- print ("不错啊,已经很近了哦!")
- elif BI <= 100:
- print ("不错嘛!正确答案已经在一百以内了")
- elif BI <= 1000:
- print ("快了快了,误差在1000以内了哦!!!")
- elif BI <= 5000:
- print ("隔了将近五千呢小伙子!!!怎么搞起在?!")
- elif BI > 5000:
- print ("你这玩意儿也差太远了吧....")
-
- print ("========怪石鸟工作室荣誉出品========")
- res = random.randint(-10000,10000)
- value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
- typ()
- value = int(value)
- if value == res:
- print ("哎呀妈呀...怎么第一遍就被你猜对了>_>这是神马狗屎运啊!!")
- else:
- print ("嘿!-_-猜错了吧!")
- end = 19
- for it in range(19):
- value = input ("嘿!小伙子!猜猜怪石鸟心里在想什么数字? :")
- typ()
- value = int(value)
- if value == res:
- print ("哎呀妈呀...怎么就被猜中了呢...>_>这是神马狗屎运啊!!")
- end = None
- break
- BI = abs( value - res )
- if value < res:
- butong()
- print("小了小了!")
- end -= 1
- else:
- butong()
- print("大了大了!")
- end -= 1
- if end == 0:
- print("都猜这么多次了!不跟你玩啦...哼!!!!")
- print("游戏结束,不跟你完啦<_>")
复制代码
然后写好代码。。。自己测试发现没有问题如图
然后本来一切都结束了。。。我发现我自己搞不懂了,我的代码应该还存在问题的啊。当我输入正数的时候如果不带+加号的话他的typ函数就会判断我输入了错误的数据了啊。
比如“5000”我的typ先判断是否以正负号开头,不是,然后就执行下面else了啊?
有点没懂,然后我自己猜测是不是字符串里是正数的话会默认带一个+加号,然后如图,自己测试发现不是,
请问哪位能解释一下为什么我的代码在输入一个不带正号的正数时不进入else,而是直接pass掉了?
|
|