缩进问题
python对t缩进非常严格
import random
times = 3
secret = random.randint(1,10)
print("--------------------")
print("猜1到10里面猜整数哦")
guess = 0
temp = input()
if (temp is int):
while (guess != secret) and (times > 2):
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp = input()
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp = input()
guess = int(temp)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("1到10整数,你输的是个啥?")
temp2 = input()
if (temp2 is int):
while (guess != secret) and (times > 2):
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp2 = input()
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp2 = input()
guess = int(temp2)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("给你机会都不珍惜")
另外判断是否为整数用 temp.isnumeric()而非temp is int
修改代码为
import random
times = 3
secret = random.randint(1,10)
print("--------------------")
print("猜1到10里面猜整数哦")
guess = 0
temp = input()
if temp.isnumeric():
while (guess != secret) and (times > 2):
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp = input()
guess = int(temp)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp = input()
guess = int(temp)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("结束了")
else:
print("1到10整数,你输的是个啥?")
temp2 = input()
if temp2.isnumeric():
while (guess != secret) and (times > 2):
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("猜大了")
else:
print("猜小了")
while guess != secret and times > 1:
print("再给你一次机会接着猜")
temp2 = input()
guess = int(temp2)
times = times - 1
if guess == secret:
print("猜对了")
else:
if guess > secret:
print("又猜大了")
else:
print("又猜小了")
while guess != secret and times > 0:
print("最后一次机会了啊")
temp2 = input()
guess = int(temp2)
times = times -1
if guess == secret:
print("终于猜对了")
else:
print("废物")
print("给你机会都不珍惜")
print("结束了")
|