English 中文(简体)
我如何把这种回返/全球驱逐回?
原标题:How Do I Fix this Return/Global Error?
def subtract(num):
    string = str(num)
    a = string[0]
    b = string[1]
    c = string[2]
    large = max(a, b, c)
    small = min(a,b,c)
    summation = int(a) + int(b) + int(c)
    mid = summation - int(large) - int(small)
    mid2 = str(mid)
    ascend = large + mid2 + small
    descend = small + mid2 + large
    print( The digits in ascending order are , ascend)
    print( The digits in descending order are , descend)
    value = int(descend) - int(ascend)
    return value
def main():
    dummy = input( Type a three digit integer, please.
 )
    if not len(dummy) == 3:
        print( Error! )
        main()
    elif not dummy.isdigit():
        print( Error! )
        main()
    if len(dummy) == 3 and dummy.isdigit():
        subtract(dummy)
        print( The value of the digits in descending order minus the digits in ascending order is , value)
main()

当我提出123个这样的意见时,我得到:

Type a three digit integer, please.
123
The digits in ascending order are 321
The digits in descending order are 123
Traceback (most recent call last):
File "/Users/philvollman/Documents/NYU/Freshman /Fall Semester/Intro to Computer Programming/Assignments/Homework5PartA.py", line 29, in <module>
main()
File "/Users/philvollman/Documents/NYU/Freshman /Fall Semester/Intro to Computer Programming/Assignments/Homework5PartA.py", line 28, in main
print( The value of the digits in descending order minus the digits in ascending order is , value2)
NameError: global name  value2  is not defined
>>> 

我不敢肯定,为什么我会这样做,因为我的第一个职能只有在发言真实的情况下才能运作,而且如果发言的话,返回的价值应该归还。

问题回答

在<代码>印/代码>末的<>印/代码>上,请参阅<编码>> 值/代码>未在<代码>>>中界定的变量。 因此错误。 或许,你打算保留从电话到/subtract的回馈价值:

value = subtract(dummy)
print( The value ... is , value)

我必须承认,很难遵循你的法典,特别是因为你所张贴的错误信息与你所张贴的法典不一致。


我认为,你的基本误解涉及如何恢复价值。 当你称之为一种回报价值的职能时,你必须把这一价值分配给称呼范围空间中的东西。

So when you wrote

subtract(dummy)

a 价值被退回,但由于你没有把它分配给任何东西,这一价值被遗忘。

相反,为了利用它,你必须把它分配给某些东西。

value = subtract(dummy)

<代码>return Value, at the end of their functionsubtract make that Value Available to the callser, it doesn t magically sent it into the callser s namespace.

Try

value2 = subtract(dummy)

......

你们正在这样做,因为你指的是主人范围未界定的变数(价值),而不是错误信息,即没有价值。

So either do

print( The value of the digits in descending 或der minus the digits in ascending 或der is , subtract(dummy))

value = subtract(dummy)
print( The value of the digits in descending 或der minus the digits in ascending 或der is , value)

从一项职能中收回价值只是回报平原价值,而不是该职能中界定的任何变数的名称。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签