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()
Quand je nombre aa d'entrée comme 123, je reçois:Comment corriger ce retour/erreur globale?
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
>>>
Je ne sais pas pourquoi je reçois ce parce que ma première fonction ne fonctionne que si l'instruction if sont vraies et la valeur retournée doit être retourné dans l'instruction if.
Le code que vous avez collé n'a pas de valeur2, donc je vais dire que ce code n'est pas identique au code que vous utilisez actuellement. – Useless