|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def symbol(s):
- list1 = []
- n = 0
- m = ''
- while n <= len(list1):
- if s[n] == '*' or s[n] == '/' or s[n] == '^' or s[n] == '+' or s[n] == '-' or s[n] == '(' or s[n] == ')':
- list1.append(s[n])
- elif '0' <= s[n] <= '9':
- m += s[n]
- if s[n + 1] != '9' and s[n + 1] != '8' and s[n + 1] != '7' and s[n + 1] != '6' and\
- s[n + 1] != '5' and s[n + 1] != '4' and s[n + 1] != '3' and s[n + 1] != '2' and\
- s[n + 1] != '1' and s[n + 1] != '0':
- list1.append(m)
- m = ''
- n += 1
- return list1
- def main():
- expression = str(input('please enter a math expression:'))
- print('The symbolization is:', symbol(expression))
- if __name__ == '__main__':
- main()
复制代码
代码是将一个数学公式分割成若干个元素,数字在一起,为什么在测试此程序的debug永远输出一个空列表,是怎么回事
本帖最后由 jackz007 于 2021-10-26 00:20 编辑
- def symbol(s):
- list1 , n , m = [] , 0 , ''
- while n < len(s) :
- if s[n] in '*/^+-()':
- if m :
- list1 . append(m)
- m = ''
- list1 . append(s[n])
- elif '0' <= s[n] <= '9':
- m += s[n]
- n += 1
- if m:
- list1 . append(m)
- return list1
- def main():
- expression = str(input('please enter a math expression:'))
- print('The symbolization is:', symbol(expression))
- if __name__ == '__main__':
- main()
复制代码
运行实况:
- D:\00.Excise\Python>python x.py
- please enter a math expression:108+209*3
- The symbolization is: ['108', '+', '209', '*', '3']
- D:\00.Excise\Python>
复制代码
楼主,是这个意思啵?
|
|