Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

import random
secret=random.randint(1,100)
print('''猜数游戏!
我想了一个1-100的整数,你最多可以猜六次
看看能猜出来吗?''')
tries=1
while tries<=6:
    guess=int(input('1-100的整数,第d次猜,请输入:' % (tries,)))
    if guess==secret:
        print('恭喜答对了!你只猜了d次!
就是这个:%d!' % (tries,secret))
        break
    elif guess>secret:
        print('不好意思,你的数大了一点!')
    else:
        print('不好意思,你的数小了一点!')
    tries +=1
else:
    print('哎呀!怎么也没猜中!再见!')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
565 views
Welcome To Ask or Share your Answers For Others

1 Answer

给你改了一下:

错误有两处:

  • 读取控制台输入的方式有问题
  • 代码格式需要调整
import random
secret=random.randint(1,100)
print('''猜数游戏!
我想了一个1-100的整数,你最多可以猜六次
看看能猜出来吗?''')
tries=1
while tries<=6:
    guess=int(input('1-100的整数,第d次猜,请输入:' ))
    if guess==secret:
        print('恭喜答对了!你只猜了d次!
就是这个:%d!' % (tries,secret))
        break;
    elif guess>secret:
        print('不好意思,你的数大了一点!')
    else:
        print('不好意思,你的数小了一点!')
    tries +=1
else:
    print('哎呀!怎么也没猜中!再见!')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...