The previous understanding of the game was completely wrong. I thought it was the player playing with the player. It turned out to be playing with the machine. Then the previous code is completely wrong. Then type a new code. The optimization idea has not changed. Similarly, although it took a lot of time to retype the code, I have a deeper understanding of loops and judgment statements:
import random
rd=random.randint(1, 1001)
min =1
max =1000
countpeo= 0
countcom= 0
countdel=0
for i in range(1,1000):
num = int(input("您输入的数字是:"))
countpeo=countpeo+1
if num>max or num<min:
print("你输出超过范围啦!")
countdel=countdel+1
else:
if num>rd:
max=num-1
print("你加入后现在的范围是:[{}-{}]".format(min, max))
machine = random.randint(min, max)
print(f"机器说的数值{machine}")
if machine>rd:
max = machine-1
elif machine<rd:
min = machine+1
else:
print(f"机器引爆了炸弹,炸弹是{rd}")
break
print("机器加入后现在的范围是:[{}-{}]".format(min, max))
countcom=countcom+1
if num<rd:
min = num+1
print("现在的范围是:[{}-{}]".format(min, max))
machine = random.randint(min, max)
print(f"机器说的数值是{machine}")
if machine>rd:
max = machine-1
elif machine<rd:
min = machine+1
else:
print(f"机器引爆了炸弹,炸弹是{rd}",end="")
print("congratulate!你胜利了")
countcom=countcom+1
print("机器加入后现在的范围是:[{}-{}]".format(min, max))
countcom=countcom+1
if num==rd:
print(f"你引爆了炸弹,炸弹是{rd}")
print("连电脑都输了,remake吧")
break
countpeo1=countpeo-countdel
countsum=countcom+countpeo1
print(f"你输入了{countpeo1}次,机器输入了{countcom}次,你们总共玩了{countsum}次")
Result graph: