python猜数字游戏问题出在哪了?
本来设计是猜错的情况有6次机会,可是每次猜一次就说6次机会用完了.importrandomsecret=random.randint(1,100)guess=0tries...
本来设计是猜错的情况有6次机会,可是每次猜一次就说6次机会用完了.
import random
secret = random.randint(1,100)
guess = 0
tries = 0
print "这是一个猜数字游戏!"
print "数字是1到99,你有六次机会!"
while guess != secret and tries < 6:
guess = input("请输入数字?")
if guess < secret:
print "数字太小...!"
elif guess >secret:
print "数字太大..."
tries = tries +1
if guess == secret:
print "恭喜你猜对了!"
else:
print "你6次机会用完了..."
print "正确的数字是",secret 展开
import random
secret = random.randint(1,100)
guess = 0
tries = 0
print "这是一个猜数字游戏!"
print "数字是1到99,你有六次机会!"
while guess != secret and tries < 6:
guess = input("请输入数字?")
if guess < secret:
print "数字太小...!"
elif guess >secret:
print "数字太大..."
tries = tries +1
if guess == secret:
print "恭喜你猜对了!"
else:
print "你6次机会用完了..."
print "正确的数字是",secret 展开
展开全部
你好:
错误的原因在于,错误的次数是tries,而你判断错了:
正确的代码如下:
# -*- coding: cp936 -*-
import random
secret = random.randint(1,100)
guess = 0
tries = 0
print "这是一个猜数字游戏!"
print "数字是1到99,你有六次机会!"
while guess != secret and tries < 6:
guess = input("请输入数字?")
if guess < secret:
print "数字太小...!"
elif guess >secret:
print "数字太大..."
elif guess == secret:
print "恭喜你猜对了!"
tries = tries +1
if tries==6:
print "你6次机会用完了..."
print "正确的数字是",secret
break
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询