求老师帮忙看下我这个代码用户输入起始区间不为1就会报错
来源:3-2 项目作业
张一并
2019-09-04 16:52:45
import random
print('*********************************欢迎来到猜数字小游戏******************************')
print('数字区间起始值:',end='')
a=input()
print('数字区间起始值:',end='')
b=input()
e='1234567890'
if a not in e and b not in e:
print('请输入数字')
else:
if a==b:
print('区间大小相同,请重新启动')
elif a>b:
print('a>b重新启动')
else:
c=random.randint(int(a),int(b))
print(c)
n=0
while True:
f=open('test.txt','a',encoding='utf-8')
g='请继续输入您的猜测的数字:\n'
h='**********************\n'
print(g)
print(h)
f.write(g)
f.write(h)
n=n+1
d=int(input())
f.write(str(d))
if d not in range (int(a),int(b)+1):
i='对不起您输入的数字不在指定区间\n'
print(i)
f.write(i)
else:
if d<c:
j='lower than the answer\n'
print(j)
f.write(j)
elif d>c:
k='heigher than the answer\n'
print(k)
f.write(k)
elif d==c:
l='恭喜您!只用了%s次就赢得了游戏\n'%n
print(l)
f.write(l)
break
f.close()1回答
同学你好:
经过我的测试之后,没有发现同学说的现象。

同学需要注意文件对象的缩进

同学可以提供自己的输入过程和报错信息,方便我们为同学解决问题。
如果我解决了同学的问题,请采纳!学习愉快^_^。
相似问题