This part of a program, I have to do. I have to take a string\"s\", which for ex
ID: 3852385 • Letter: T
Question
This part of a program, I have to do. I have to take a string"s", which for example might be s= " 3 * 5 + 1 " and properly calculate it. I need some help getting started, thanks. Also in python 3.6
def getNextNumber(expr, pos):
#expr is a given arithmetic formula in string s
#pos = start position in expr
#1st returned value = the next number (None if N/A)
#2nd returned value = the next operator (None if N/A)
#3rd retruned value = the next operator position (None if N/A)
if len(expr)==0 or not isinstance(expr, str) or pos<0 or pos>=len(expr) or not isinstance(pos, int):
print("type mismatch error: getNextNumber")
return None, None, "type mismatch error: getNextNumber"
#--- function code starts ---#
#--- function code ends ---#
Explanation / Answer
#--- function code starts ---#
def getNextNumber(expr,pos):
if len(expr)==0 or not isinstance(expr,str) or not isinstance(pos,int):
print("type mismatch error: getNextNumber")
return (None,None,"type mismatch error: getNextNumber")
elif pos<=0 or pos>=len(expr):
print("type mismatch error: getNextNumber")
return (None,None,"type mismatch error: getNextNumber")
else:
#print ("position",expr.index(pos))
return (pos)
#--- function code ends ---#
s = input('Please enter expression')
s= " 3 * 5 + 1 "
for letter in s:
print (letter,s.index(letter))
res=getNextNumber(s,s.index(letter))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.