<p>我想在类之外获取一个变量,但不能在文件之外。我在课堂之外有一个条件,但我也必须在课堂上使用它。我能做吗?你知道吗</p>
<p>这是要尝试的样本,如果它是工作的。我想删除输入部分和做全局变量。你知道吗</p>
<pre><code>class ComplexMethods:
ask = input("What type you are writing? (absolute value and phase angle or real and imaginary parts)")
if ask == "real and imaginary parts":
</code></pre>
<p>我试过这个但没用。它给出的名称“ask”没有定义。你知道吗</p>
<pre><code>class ComplexMethods:
global ask
if ask == "real and imaginary parts":
</code></pre>
<p>这是课外活动。你知道吗</p>
<pre><code>ask = input("What type you are writing? (absolute value and phase angle or real and imaginary parts)")
if ask == "real and imaginary parts":
firstcomplexreal = float(input("Enter real part of first complex number: "))
firstcompleximaginary = float(input("Enter imaginary part of first complex number: "))
secondcomplexreal = float(input("Enter real part of second complex number: "))
secondcompleximaginary = float(input("Enter imaginary part of second complex number: "))
complexnumbers = ComplexMethods(firstcomplexreal, firstcompleximaginary, secondcomplexreal,
secondcompleximaginary)
</code></pre>