学到哪 发表于 2020-2-2 16:59:48

课后练习4的问题求教

import random
time = 3
secret=random.randint(1,10)
print('-----------课后练习4-----------')
guess = 0
print('猜一下今天的幸运数字是多少:',end='')
while (guess != secret) and (times > 0):
    temp = input()
    guess = int(temp)
    times = times - 1
    if guess == secret:
      print('你真聪明')
      print('可惜你猜中我 也不会给你奖励')
    else:
      if guess >secret:
            print('哼,不按要求来')
      else:
            print('再试一次,再试一次')
      if times > 0:
            print('再给最后一次机会:', end = '55')
      else:
            print('机会用完啦')               
print('游戏结束,不玩了。')

运行报错:
   

qiuyouzhi 发表于 2020-2-2 21:43:22

第二行:times

傻眼貓咪 发表于 2021-9-7 22:23:17

def game():
    """
    課後練習 4
    """
    import random
    secret = random.randint(1, 10); n = 3
    while n:
      try:
            num = int(input("guess a number between 1 to 10: "))
            n -= 1
            if num == secret: print("correct answer!"); return
            elif num > secret and n: print("too big, please try again"); continue
            elif n: print("too small, please try again"); continue
            else: print("you are lose"); return
      except: print("invalid input, try again"); continue

print(game.__doc__)
game()
页: [1]
查看完整版本: 课后练习4的问题求教