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.\n')
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
>>>
Я не уверен, почему я получаю это, потому что моя первая функция запускается только в том случае, если операторы if истинны, а возвращаемое значение должно быть возвращено в операторе if.