• 简单的加减法


    编写math_game.py脚本,实现以下目标:

    1. 随机生成两个100以内的数字
    2. 随机选择加法或是减法
    3. 总是使用大的数字减去小的数字
    4. 如果用户答错三次,程序给出正确答案
      from random import randint, choice
      def exam(): counter = 0 while counter < 3: nums = [randint(1, 100) for i in range(2)] nums.sort(reverse=True) op = choice('+-') if op == '-': result = nums[0] - nums[1] else: result = nums[0] + nums[1] prompt = '%s %s %s =' % (nums[0], op, nums[1]) answer = int(input(prompt)) if answer == result: print('very Good') else: print('33[31;1msorry,You are wrong33[0m') counter += 1 def main(): while True: yn = input('Continue:/y/n ?').strip()[0] # 去除空白 然后取第一位 if yn in 'nN': print(' Bye-bye') break exam() if __name__ == '__main__': main()
      
      
      from random import randint, choice


      def add(x,y):
      return x-y
      def sum(x,y):
      return x+y

      def exam():

      cmds = {'-':add,'+':sum}
      nums = [randint(1, 100) for i in range(2)]
      nums.sort(reverse=True)
      op = choice('+-')
      result = cmds[op](*nums)
      # print(*nums)
      # if op == '-':
      # result = add(nums[0],nums[1])
      # else:
      # result = sum(nums[0],nums[1])
      prompt = '%s %s %s =' % (nums[0], op, nums[1])

      counter = 0
      while counter < 3:
      try:
      answer = int(input(prompt))
      except:
      print()
      continue
      if answer ==result :
      print('very Good')
      break
      else:
      print('33[31;1msorry,You are wrong33[0m')
      counter += 1
      else:
      print('%s %s' %(prompt,result))


      def main():

      while True:
      exam()
      try:
      yn = input('Continue:/y/n ?').strip()[0] # 去除空白 然后取第一位
      except IndexError:
      continue
      except (KeyboardInterrupt,EOFError):
      yn = 'n'

      if yn in 'nN':
      print(' Bye-bye')
      break



      if __name__ == '__main__':
      main()
       
  • 相关阅读:
    jQuery的deferred对象详解
    2016.7.15见闻
    如何在win7下配置IIS?
    对于transform的新认识
    移动前端工作的那些事---前端制作之微信小技巧篇
    css改变图片的颜色
    对promise的简单理解
    小程序打印饼图报错VM6541:1 thirdScriptError Converting circular structure to JSON;
    小程序使用echarts 在一个页面打印多个饼图的坑
    小程序使用wx.navigateTo无法跳转到加了tabBar的页面
  • 原文地址:https://www.cnblogs.com/lsgo/p/10554527.html
Copyright © 2020-2023  润新知