python猜数字游戏求助
import randoma = random.randint(0.10)
temp = input("输入数字")
while not temp.isdigit():
print("输入有误,请输入数字")
temp = input("重新输入")
break
guess = 0
time = 3
while (guess != a) and (time > 0):
temp = input()
guesss = int(temp)
time = time - 1
if guess == a:
print("猜中了")
print("游戏结束")
else:
if guess > a:
print("大了")
else:
print("小了")
print("游戏结束")
以上是代码,想做一个随机生成1到10的猜数字游戏,只有三次机会,还要判断是否为数字,结果代码运行不了,想问下哪里错了,拜托了 本帖最后由 jackz007 于 2020-12-17 15:48 编辑
多头输入,多头判断,导致程序逻辑错乱,明明在代码开头输入了数据,却还要在后面的循环里重新输入。
temp = input("输入数字")
while not temp.isdigit():
print("输入有误,请输入数字")
temp = input("重新输入")
break
guess = 0
这些代码完全是废码,不仅无用,而且添乱,可以直接删除。
好的代码应该做到统一输入、统一判断、统一输出信息,这样,程序代码才会逻辑清晰,不易出错。
下面的代码经过梳理和修改,谨供楼主参考
import random
a , times = random . randint(1 , 10) , 3
while times > 0:
while True:
temp = input("输入数字")
if temp . isdigit():
break
else:
print("输入有误,请输入数字")
guess = int(temp)
times = times - 1
if guess == a:
print("猜中了")
break
else:
if guess > a:
print("大了")
else:
print("小了")
print("游戏结束") import random
a = random.randint(0,10)
temp = input("输入数字")
while temp.isdigit() == False:
print("输入有误,请输入数字")
temp = input("重新输入:")
guess = int(temp)
time = 3
while (guess != a) and (time > 0):
temp = input('请重新输入:')
time = time - 1
if guess == a:
print("猜中了")
print("游戏结束")
else:
if guess > a:
print("大了")
else:
print("小了")
print("游戏结束")
你看看这样行不行
谢谢两位,非常感谢
页:
[1]