基础知识

  1. print("冒号和缩进是一种语法,区分代码之间的层次,缩进是四个空格或一个Tab键")

样例代码

识别图片

  1. import requests
  2. from aip import AipOcr
  3. image = requests.get('https://res.pandateacher.com/python_classic.png').content
  4. APP_ID = '16149264'
  5. API_KEY = 'yxYg9r4OuAs4fYvfcl8tqCYd'
  6. SECRET_KEY = 'yWg3KMds2muFsWs7MBSSFcgMQl8Wng4s'
  7. client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
  8. res = client.basicGeneral(image)
  9. if 'words_result' in res.keys():
  10. for item in res['words_result']:
  11. print(item['words'])
  12. else:
  13. APP_ID = '11756541'
  14. API_KEY = '2YhkLuyQGljPUYnmi1CFgxOP'
  15. SECRET_KEY = '4rrHe2BF828bI8bQy6bLlx1MelXqa8Z7'
  16. client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
  17. res = client.basicGeneral(image)
  18. if 'words_result' in res.keys():
  19. for item in res['words_result']:
  20. print(item['words'])
  21. else:
  22. print(res)

简单聊天

  1. import urllib.parse
  2. import hashlib
  3. import time
  4. import random
  5. import string
  6. import requests
  7. import sys
  8. import readline
  9. def curl_md5(src):
  10. m = hashlib.md5(src.encode('UTF-8'))
  11. # 将得到的MD5值所有字符转换成大写
  12. return m.hexdigest().upper()
  13. """
  14. get_req_sign :根据 接口请求参数 和 应用密钥 计算 请求签名
  15. 参数说明
  16. -pa:接口请求参数
  17. -apk:应用密钥
  18. 返回数据
  19. -签名结果
  20. """
  21. def get_req_sign(pa, apk):
  22. # 1.字典升序排序
  23. pa_list = list(pa.items())
  24. pa_list.sort()
  25. # 2.拼按URL键值对
  26. tem_str = ''
  27. for i in range(len(pa_list)):
  28. if pa_list[i][1]:
  29. tem_str += (pa_list[i][0] + '=' + urllib.parse.quote(pa_list[i][1]) + '&')
  30. # 3.拼接app_key
  31. tem_str += ('app_key=' + apk)
  32. # 4.MD5运算 + 转换大写,得到请求签名
  33. sign = curl_md5(tem_str)
  34. return sign
  35. def get_params(plus_item):
  36. # 请求时间戳(秒级),用于防止请求重放(保证签名5分钟有效)
  37. t = time.time()
  38. time_stamp = str(int(t))
  39. # 请求随机字符串,用于保证签名不可预测
  40. nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))
  41. global se
  42. app_id = '2127007340'
  43. app_key = '9Fm2D9tkB4lEjZ89'
  44. params = {
  45. 'app_id': app_id,
  46. 'time_stamp': time_stamp,
  47. 'nonce_str': nonce_str,
  48. 'question': plus_item,
  49. 'sign': '',
  50. 'session': se
  51. }
  52. params['sign'] = get_req_sign(params, app_key)
  53. return params
  54. def get_content(plus_item):
  55. # 聊天的API地址
  56. url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat"
  57. # 获取请求参数
  58. plus_item = plus_item.encode('UTF-8')
  59. payload = get_params(plus_item)
  60. r = requests.post(url, data=payload)
  61. if r.json()["ret"] == 0:
  62. print('吴小枫:' + r.json()["data"]["answer"])
  63. else:
  64. print('今天我出了一点小问题~快联系助教帮忙解决一下~~')
  65. sys.exit()
  66. feature_text = ['''
  67. 大家好!我是你的聊天机器人吴小枫。
  68. 我有问必答,有人会问我“今天深圳天气怎么样?”,也有人问我“你喜欢我吗?”
  69. 快来问我问题呀,欢迎来撩!
  70. >''', '''
  71. 再来问我点啥吧!我把我知道的都告诉你,嘻嘻!
  72. >''', '''
  73. # 我有点饿了,再和你聊完最后一句,我就要下线啦!你还有什么要问我的?
  74. # >''']
  75. se = str(random.randint(1, 10000))
  76. for i in range(len(feature_text)):
  77. comment = input(feature_text[i])
  78. get_content(comment)
  79. time.sleep(1)
  80. print('\n我走啦,下次见!')

猜数字

  1. import random
  2. import time
  3. ###提示语部分
  4. print('你好,我是机器人小埋,我们来玩个猜年龄的小游戏吧~(◆◡◆)')
  5. time.sleep(2)
  6. print('''
  7. =============================
  8. 干物妹!うまるちゃんの年齢
  9. =============================
  10. ''')
  11. time.sleep(1)
  12. print('小埋的真实年龄在1到10之间哦~')
  13. time.sleep(1)
  14. print('不过,你只有5次机会哦~')
  15. time.sleep(1)
  16. print('下面,请输入小埋的年龄吧:')
  17. #从0至10产生一个随机整数,并赋值给变量age
  18. age = random.randint(1,10)
  19. #设置次数
  20. for guess in range(1,6):
  21. #输入玩家猜测的年龄
  22. choice=int(input())
  23. #判读玩家输入的年龄是否等于正确的年龄
  24. if choice<age:
  25. print('小埋的提示:你猜小了(;´д`)ゞ。。。。')
  26. elif choice>age:
  27. print('小埋的提示:乃猜大了惹(>﹏<)~~')
  28. else:
  29. print('猜了'+str(guess)+'次,你就猜对惹~hiu(^_^A;)~~~')
  30. break
  31. #判断猜测次数
  32. if choice == age:
  33. print('搜噶~那么小埋下线了~拜拜~( ̄︶ ̄)↗')
  34. else:
  35. print('哎呀~你还是木有猜对啊~但是你只有5次机会诶~怎么办啊~')
  36. print('那好吧~心软的小埋只好告诉你,我才'+str(age)+'岁哦~(*/ω\*)')