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