为什么总是返回一个空列表
def symbol(s):list1 = []
n = 0
m = ''
while n <= len(list1):
if s == '*' or s == '/' or s == '^' or s == '+' or s == '-' or s == '(' or s == ')':
list1.append(s)
elif '0' <= s <= '9':
m += s
if s != '9' and s != '8' and s != '7' and s != '6' and\
s != '5' and s != '4' and s != '3' and s != '2' and\
s != '1' and s != '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永远输出一个空列表,是怎么回事 给一个你输入的公式 suchocolate 发表于 2021-10-26 00:07
给一个你输入的公式
15-5=10 本帖最后由 jackz007 于 2021-10-26 00:20 编辑
def symbol(s):
list1 , n , m = [] , 0 , ''
while n < len(s) :
if s in '*/^+-()':
if m :
list1 . append(m)
m = ''
list1 . append(s)
elif '0' <= s <= '9':
m += s
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>
楼主,是这个意思啵? jackz007 发表于 2021-10-26 00:17
运行实况:
楼主,是这个意思啵?
对是这个意思
1005663861 发表于 2021-10-26 00:23
对是这个意思
那你的问题就解决了,别忘记 "最佳答案" 哦{:5_109:}
页:
[1]