0
  1. I need to run a code using 'loop for' 20 times.
  2. If there is no user input (enter), my code should print "Exit program~~" and break the loop.
  3. If the code is ran 20 times already, the code should print "Exit program~~" AFTER printing the last (2oth) input number.
  4. I need '+/- number' to be printed after every time user inputs the number.
  5. The output must be printed with str.format()

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]))
9
  • Welcome to SO. Perhaps my understanding of your problem is faulty, but couldn't you just add the print Exit program after the loop? Commented Apr 13, 2022 at 3:55
  • There's no need to use format() when you're just printing a single string. '{}'.format('Exit program~~') should just be 'Exit program~~' Commented Apr 13, 2022 at 3:57
  • @jrylim ewong gave a suggestion to fix the problem. Did you try it? Commented Apr 13, 2022 at 4:01
  • The Exit program was printed double when I did that, I fixed it by removing the print Exit program above the break. Thanks! @ewong Commented Apr 13, 2022 at 4:04
  • I tried and the Exit program was printed double. But I managed to fix it thanks to Aproximate Knowledge's help! @Code-Apprentice Commented Apr 13, 2022 at 4:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Simply

print('Exit program~~')

right after the for loop. So after the for loop runs 20 times, Exit program~~ will be printed.

1 Comment

It made Exit program printed double,, but thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.