子沙 发表于 2018-7-28 17:10:41

Python猜数字小游戏(新手上路)

本帖最后由 子沙 于 2018-7-28 17:13 编辑

import random
secret=random.randrange(1,101)
print('The integer is between 1 and 100.')
times=int(input('How many times do you want to guess?\n'))
guess=int(input('Enter a integer:'))
i=1   
while i<times:
    if guess==secret:
      if i==1:
            print('You only use',i,'chance.')
      else:
            print('You only use',i,'chances.')
      i=times
    else:
      if guess<secret:
            print('No, it is a little higher than that.(',i,'/',times,')')
      else:
            print('No, it is a little lower than that.(',i,'/',times,')')
      guess=int(input('Enter a integer:'))
      i=i+1
if guess==secret:
    print('Congratulations, you guessed it.')
    print('Game is over.')
else:
    print('Sorry, you fail the game.(',i,'/',times,')')
    print('Game is over. Thanks')
页: [1]
查看完整版本: Python猜数字小游戏(新手上路)