Output should be like this:
Enter the number : 0
+0 : zero
Enter the number : 17
+17 : positive
Enter the number : -8
-8 : negative
Enter the number:
Exit program~~
My problem: My code works, but I can't figure out how to print "Exit program~~" after I input numbers 20 times. *But "Exit program~~" works when I exit before inputting all 20 times.
My code:
sign = ['positive', 'negative', 'zero']
for i in range(20):
num = input('Enter the number : ')
if num == '':
print('{}'.format('Exit program~~'))
break
else:
if int(num) > 0:
print('{:+.0f} : {}'.format(int(num), sign[0]))
else:
if int(num) < 0:
print('{:-.0f} : {}'.format(int(num), sign[1]))
else:
print('{:+.0f} : {}'.format(int(num), sign[2]))
sign = ['positive', 'negative', 'zero']
for i in range(20):
num = input('Enter the number : ')
if num == '':
break
else:
if int(num) > 0:
print('{:+.0f} : {}'.format(int(num), sign[0]))
else:
if int(num) < 0:
print('{:-.0f} : {}'.format(int(num), sign[1]))
else:
print('{:+.0f} : {}'.format(int(num), sign[2]))
print('{}'.format('Exit program~~'))
Have you considered something like this? Just removing the print for the case where nothing is entered and just having one print for anytime the loop is exited.