鱼C论坛

 找回密码
 立即注册
查看: 1817|回复: 5

[已解决]为什么总是返回一个空列表

[复制链接]
发表于 2021-10-25 23:42:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. def symbol(s):
  2.     list1 = []
  3.     n = 0
  4.     m = ''
  5.     while n <= len(list1):
  6.         if s[n] == '*' or s[n] == '/' or s[n] == '^' or s[n] == '+' or s[n] == '-' or s[n] == '(' or s[n] == ')':
  7.             list1.append(s[n])
  8.         elif '0' <= s[n] <= '9':
  9.             m += s[n]
  10.             if s[n + 1] != '9' and s[n + 1] != '8' and s[n + 1] != '7' and s[n + 1] != '6' and\
  11.                     s[n + 1] != '5' and s[n + 1] != '4' and s[n + 1] != '3' and s[n + 1] != '2' and\
  12.                     s[n + 1] != '1' and s[n + 1] != '0':
  13.                 list1.append(m)
  14.                 m = ''
  15.     n += 1
  16.     return list1


  17. def main():
  18.     expression = str(input('please enter a math expression:'))
  19.     print('The symbolization is:', symbol(expression))


  20. if __name__ == '__main__':
  21.     main()
复制代码


代码是将一个数学公式分割成若干个元素,数字在一起,为什么在测试此程序的debug永远输出一个空列表,是怎么回事
最佳答案
2021-10-26 00:17:36
本帖最后由 jackz007 于 2021-10-26 00:20 编辑
  1. def symbol(s):
  2.     list1 , n , m = [] , 0 , ''
  3.     while n < len(s) :
  4.         if s[n] in '*/^+-()':
  5.             if m :
  6.                 list1 . append(m)
  7.                 m = ''
  8.             list1 . append(s[n])
  9.         elif '0' <= s[n] <= '9':
  10.             m += s[n]
  11.         n += 1
  12.     if m:
  13.         list1 . append(m)
  14.     return list1

  15. def main():
  16.     expression = str(input('please enter a math expression:'))
  17.     print('The symbolization is:', symbol(expression))

  18. if __name__ == '__main__':
  19.     main()
复制代码

        运行实况:
  1. D:\00.Excise\Python>python x.py
  2. please enter a math expression:108+209*3
  3. The symbolization is: ['108', '+', '209', '*', '3']

  4. D:\00.Excise\Python>
复制代码

        楼主,是这个意思啵?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-26 00:07:56 | 显示全部楼层
给一个你输入的公式
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-26 00:10:26 | 显示全部楼层
suchocolate 发表于 2021-10-26 00:07
给一个你输入的公式

15-5=10
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 00:17:36 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-10-26 00:20 编辑
  1. def symbol(s):
  2.     list1 , n , m = [] , 0 , ''
  3.     while n < len(s) :
  4.         if s[n] in '*/^+-()':
  5.             if m :
  6.                 list1 . append(m)
  7.                 m = ''
  8.             list1 . append(s[n])
  9.         elif '0' <= s[n] <= '9':
  10.             m += s[n]
  11.         n += 1
  12.     if m:
  13.         list1 . append(m)
  14.     return list1

  15. def main():
  16.     expression = str(input('please enter a math expression:'))
  17.     print('The symbolization is:', symbol(expression))

  18. if __name__ == '__main__':
  19.     main()
复制代码

        运行实况:
  1. D:\00.Excise\Python>python x.py
  2. please enter a math expression:108+209*3
  3. The symbolization is: ['108', '+', '209', '*', '3']

  4. D:\00.Excise\Python>
复制代码

        楼主,是这个意思啵?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-26 00:23:19 | 显示全部楼层
jackz007 发表于 2021-10-26 00:17
运行实况:

        楼主,是这个意思啵?

对是这个意思
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 00:38:44 | 显示全部楼层

        那你的问题就解决了,别忘记 "最佳答案" 哦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-14 17:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表