Program to top-up phone with conditions in Python I am fairly new to Python. I wrote a program to take the user inputs which are service, the top-up time, the top-up amount, and exit or stay on the program.
Please review my code. And, can my code be organized? I think it looks a bit messy, maybe it can be shorter?

First, I wrote everything under function, so I can restart the program later on.
Second, I want to take user input about the service that they want to top up by limiting the choice of service to only 3 which are Green, Blue, or Red. If none of it, display &quot;Try Again!, Wrong Service&quot; otherwise, &quot;Welcome to Top-up Service&quot;.
Third, after passed service versification, the user can top up but limit to only 50,100,300,500,1000. Also, the user can only choose &quot;How many times they want to top-up&quot;.
Forth, after top-up, the list of top-up and sum of top-up is presented.
Last, the user can choose to stay or exit the program. If stay, the program is restarted, if exit, the program is terminated.

Here is the code that I wrote
  def main():
# Letting user choose a service
    service = None
    while service not in {&quot;Green&quot;, &quot;Blue&quot;, &quot;Red&quot;}:
        service = input(&quot;Please enter Green, Blue or Red: &quot;)
    # Now service is either Green, Blue or Red 
        if service in {&quot;Green&quot;, &quot;Blue&quot;, &quot;Red&quot;}:
            print(&quot;Welcome to Top-up Service&quot;)
        else:
            print(&quot;Try Again!, Wrong Service&quot;)

# Doing Top up(s)
    top_list = []
    n=int(input(&quot;How many times do you want to top up? &quot;))
    for i in range(1,n+1):
        top_up = None 
        while top_up not in {50,100,300,500,1000}:
            top_up = int(input(&quot;Please enter 50,100,300,500 or 1000: &quot;))
    # Now top_up is either 50,100,300,500 or 1000&quot;
            if top_up in {50,100,300,500,1000}: 
                top_list.append(top_up)
            else:
                print(&quot;Try Again!, Wrong Top-up&quot;)

# Show all top up(s) information &amp; Sum all top up(s)
    print(&quot;Top up(s) information : &quot;, top_list) 
    print(&quot;Total top up(s): &quot;, sum(top_list))
    
# Letting user choose to stay or exit
    restart = input(&quot;Do you want to start again?,Y or else &quot;).upper()
    if restart == &quot;Y&quot;:
        main()
    else:
        print(&quot;Thank you for using our service&quot;)
        exit()
main()

This is the example of output
Please enter Green, Blue or Red: o
Try Again!, Wrong Service
Please enter Green, Blue or Red: Blue
Welcome to Top-up Service
How many times do you want to top up? 2
Please enter 50,100,300,500 or 1000: 20
Try Again!, Wrong Top-up
Please enter 50,100,300,500 or 1000: 50
Please enter 50,100,300,500 or 1000: 500
Top up(s) information :  [50, 500]
Total top up(s):  550
Do you want to start again?,Y or else n
Thank you for using our service
You can divide your whole program into parts, this way it becomes easy while debugging and increases modularity in the code.
You can divide into multiple functions like

Selecting service.
Add top-up
Restart logic

Refractored code
AVAILABLE_SERVICES = {&quot;Green&quot;, &quot;Blue&quot;, &quot;Red&quot;} 
AVAILABLE_TOPUPS = {50, 100, 300, 500, 1000}


def select_service():
    while True:
        service = input(&quot;Please enter Green, Blue or Red: &quot;).title()
        if service in AVAILABLE_SERVICES:
            print(&quot;Welcome to Top-up Service&quot;)
            break
    return service


def top_up(service):
    times_topup = int(input(&quot;How many times do you want to top up? &quot;))
    topups_selected = []
    for _ in range(times_topup):
        while True:
            topup = int(input(&quot;Please enter 50,100,300,500 or 1000: &quot;))
            if topup in AVAILABLE_TOPUPS:
                topups_selected.append(topup)
                break
            else:
                print(&quot;Try Again!, Wrong Top-up&quot;)

    print(&quot;Top up(s) information : &quot;, topups_selected)
    print(&quot;Total top up(s): &quot;, sum(topups_selected))


def process():
    restart = &quot;Y&quot;
    while restart == &quot;Y&quot;:
        service = select_service()
        top_up(service)
        restart = input(&quot;Do you want to start again?,Y or else: &quot;).upper()
    print(&quot;Thank you for using our service&quot;)


if __name__ == &quot;__main__&quot;:
    process()

Output:
Please enter Green, Blue or Red: Blue
Welcome to Top-up Service
How many times do you want to top up? 2
Please enter 50,100,300,500 or 1000: 7
Try Again!, Wrong Top-up
Please enter 50,100,300,500 or 1000: 50
Please enter 50,100,300,500 or 1000: 10
Try Again!, Wrong Top-up
Please enter 50,100,300,500 or 1000: 100
Top up(s) information :  [50, 100]
Total top up(s):  150
Do you want to start again?,Y or else: Y
Please enter Green, Blue or Red: Red
...
...
...
Do you want to start again?,Y or else: n
Thank you for using our service

Code Review

PEP-8 violations:

E231 - missing whitespace after ,

range(1,n+1) ⟶ range(1, n+1)
{50,100,300,500,1000} ⟶ {50, 100, 300, 500, 1000}




Declare constants for services and top-ups instead of writing {&quot;Blue&quot;, ...} and {50, ...} everywhere.

{&quot;Blue&quot;, ...} ⟶ AVAILABLE_SERVICES = {&quot;Green&quot;, &quot;Blue&quot;, &quot;Red&quot;} Now you can use AVAILABLE_SERVICES instead of writing {&quot;Blue&quot;, ...}
{50, ...} ⟶ AVAILABLE_TOPUPS = {50, 100, 300, 500, 1000}


Use str.title when taking input for service, so &quot;BLUE&quot; would become &quot;Blue&quot;.
Use _ here for i in range(1, n+1) when you are not using i in the loop.

for i in range(1, n+1) ⟶ for _ in range(n)
What's the purpose of _ in python


You can make AVAILABLE_SERVICES as frozenset to make immutable.

AVAILABLE_SERVICES = frozenset({&quot;Green&quot;, &quot;Blue&quot;, &quot;Red&quot;})
AVAILABLE_TOPUPS = frozenset({50, 100, 300, 500, 1000})




You can use pep8online* to check if your code is in accordance with PEP-8 standard.
You can use black* (code formatting tool)
black --line-length 79 your_file.py


* I'm not affiliated to pep8online and black.