Python Forum

Full Version: How to run code again in module ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
I'm a new to python and coding.

How to run back the code when a user chooses an option ?

- the code i have currently-
print('Now you try')
print('This is how to get the volumetric weight for cargo space calculation')


num1 = input('Enter length number: ')
num2 = input('Enter width number: ')
num3 = input('Enter height number: ')
sum = float(num1) * float(num2) * float(num3) / 5000

print('This is for your shipment information')

char1 = input ('Please enter your name: ')
char2 = input ('Please enter comp name: ')
char3 = input ('Please enter reference: ')

char4 = input ('Please enter you consignee name: ')
char5 = input ('Please enter consignee comp name: ')
char6 = input ('Please enter shipment value: ')

print ('Conclusion')

print('The sum of {0} and {1} and {2} divided by 5000 is = {3} cm3'.format(num1, num2, num3, sum))
print('Your name is {0}'.format(char1, ))
print('Your company is {0}'.format(char2, ))
print('Your reference is {0}'.format(char3, ))
print('Your consignee name is {0}'.format(char4, ))
print('Your consignee company name is {0}'.format(char5, ))
print('Your shipment value is ${0}'.format(char6, ))


print('What do you want to do next ?')

choice = input("""
                      A: Continue
                      B: Logout

                      Please enter your choice: """)

if choice == "A" or choice =="a":restart("test1.py",) stuck here, I don't know which command to use to loop back the code on top
elif choice == "B" or choice =="b":quit() - this works.
else:
        print("You must only select either A or B")
        print("Please try again")
        menu()
Put all the code you want to re-run inside a function. Call the function from inside a loop.
def my_func():
    # All the code I want to run over an over

while True:
    choice = get_input() # Replace with you code to read user choice
    if choice == Logout:
        break
    my_func()
To show code a example that works fine for task like this
So it's like deanhystad code taken a little futher,
so menu function is where start and always fall back into after run a function.
Then can restart an other task or Quit out.
from rich import print

def cargo_calc():
    '''This is how to get the volumetric weight for cargo space calculation'''
    num1 = input('Enter length number: ')
    num2 = input('Enter width number: ')
    num3 = input('Enter height number: ')
    return float(num1) * float(num2) * float(num3) / 5000

def shipment_calc():
    '''Not finish yet'''
    pass

def menu_choies():
    return '''\
    (1) Cargo space calculation)
    (2) shipment space calculation)
    ([bold red]Q[/bold red]) Quit\n'''

def menu():
    while True:
        print(menu_choies())
        choice = input('Enter your choice: ').lower()
        if choice == '1':
            print(f'Volumetric weight for cargo is {cargo_calc()}\n')
        elif choice == '2':
            shipment_calc()
        elif choice == 'q':
            return
        else:
            print(f'Not a correct choice: {choice},try again\n')

if __name__ == '__main__':
    menu()
Also trown in Rich for some colors which work for on all OS/shell even cmd🧵.
[Image: wWvxiv.png]