Python Forum
How to make a program run again after end?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a program run again after end?
#1
Hey there,
I made a program for collatz conjecture, wich basically:
stops when n=1
if n is dividable by 2 = divide by 2
if n isn't = 3*n+1.
here it is:

print 'Write a number, then press Enter, I will show you the way to 1!'
n=int(input('Enter here:'))
Steps = 0
while 1>0:
    if n == 1:
        print '1'
        break
    remainder = n % 2
    if remainder == 0:
        print n
        n = n/2
    if remainder == 1:
        print n
        n = n*3+1
    Steps = Steps+1
answer = str('=== it took')
answer2 = str('steps to get to 1 ===')
print answer, Steps, answer2
ok, but when it finishes, in the shell it stops with >>>, where I can read the output.
if I save it, after the program finishes, it closes the window immediatelly. is there any way to make it run again? (request a new output)
Reply
#2
Looks like you're just typing into the interpreter directly.
You can download a free IDE and write your programs from within.
If you do that, you can run by pressing a run button.

I would suggest googling 'python ide'

Personally I like PyCharm. There is a community edition available, and if you really
like it, the commercial version is available for an annual fee.

You can run the community version perfectly well without ever switching.
you can get it here

What OS are you running?
on what platform (if Linux)

Normally from a command line (or terminal), you would type: python mysavedname.py to run your saved program
Reply
#3
You could try putting your code in a 'try/except' statement, then it will run continuously until a certain condition is met.

May I ask, are you using python 2 or python 3?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
Use functions,a menu function that run i a loop.
You always fall back into this function,
which ask if want to run again or quit.
Eg:
def calc():
    print 'Write a number, then press Enter, I will show you the way to 1!'
    n = int(raw_input('Enter here:'))
    Steps = 0
    while 1 > 0:
        if n == 1:
            print '1'
            break
        remainder = n % 2
        if remainder == 0:
            print n
            n = n / 2
        if remainder == 1:
            print n
            n = n * 3+1
        Steps = Steps + 1
    answer = '=== it took'
    answer2 = 'steps to get to 1 ==='
    return answer, Steps, answer2

def show_options():
     print('\n1. Run way to 1!')
     print('(q) Quit\n')

def menu():
    while True:
        show_options()
        choice = raw_input('Enter your choice: ').lower()
        if choice == '1':
             calc()
        elif choice == 'q':
            return False
        else:
            print('Not a correct choice: {}'.format(choice))

menu()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,129 Feb-25-2022, 06:31 PM
Last Post: jao
  How to make a Vocal synthesizer program on Python? Seadust 3 3,490 Jan-28-2021, 06:26 PM
Last Post: gonnach
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,258 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
  I try to make Heron formula program abcd 7 3,329 Oct-22-2020, 12:48 AM
Last Post: abcd
  Make a Python program executable in Windows Pedroski55 1 2,067 Sep-26-2020, 12:34 AM
Last Post: bowlofred
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,685 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  how to make a program with a certain number of multiples? syafiq14 3 2,696 Jan-01-2020, 02:39 PM
Last Post: syafiq14
  How to make a program run "online"? DanielTkach 2 2,499 Apr-09-2019, 11:59 PM
Last Post: DanielTkach
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,099 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Can I make this program on Python? Cergal0 8 6,690 Dec-05-2017, 01:41 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020