<blockquote>
<p>Why is x not defined when I run the program and enter an expression?</p>
</blockquote>
<p><code>x</code>不在作用域中,您只在方法中定义它,并尝试在其他地方访问它。</p>
<pre><code>z= (input("expression:")).strip()
def finding(z):
# ... removed your code ...
# in this method, you define x, which is local
# to the method, nothing outside this method has
# access to x
return x
def Parsing(z,x):
x= finding(z) # this is a different x that is assigned the
# return value from the 'finding' method.
qw=z.s[0:x] # I'm curious as to what is going on here.
print (qw)
# take the x-value from function finding(z) and use it to split
finding(z) # here, z is the value from the top of your code
Parsing(z,x) # here, x is not defined, which is where you get your error.
</code></pre>
<p>因为<code>Parsing</code>已经在调用<code>finding</code>来获取<code>x</code>的值,所以不需要将其传递到<code>Parsing</code>,也不需要在<code>Parsing</code>外部调用<code>finding(z)</code>,因为您不在任何地方存储该值。</p>
<pre><code>def Parsing(z):
x= finding(z) # this is a different x that is assigned the
# return value from the 'finding' method.
qw=z.s[0:x] # I'm curious as to what is going on here.
print (qw)
# take the x-value from function finding(z) and use it to split
# finding(z) -- not needed
Parsing(z)
</code></pre>