-1

I would like to know how can I re-run this program. After succeful process I want to offer an opportunity to use this program again without closing and re-opeing. How can I do it? After run this code it just open window of python and instantly close it.

def sifra():
retezec = input("Zadejte slovo: ")
print("Zadali jste slovo: ",retezec)
zprava = 0
posun = int(input("Zadejte číslo o kolik se má šifra posouvat: "))

for znak in retezec:
    i = ord(znak)
    i = i + posun
    if (i > ord(z)):
        i = i - 26
    znak = chr(i)
    zprava = zprava + znak
print("Zašfrovaná zpráva: ", zprava)

znovu = input("Znovu? A/N")
if(znovu == "A" or "A"):
    sifra()
elif(znovu == "N" or "n"):
    sys.exit(0)
else:
    pass

This code work... After open, it shows window when I can enter word and then it does caesar cipher as it should but I dont have a chance to see the result because it close so fast.

retezec = input("Zadejte slovo: ")
print("Zadali jste slovo: ",retezec)
zprava = 0
posun = int(input("Zadejte číslo o kolik se má šifra posouvat: "))

for znak in retezec:
    i = ord(znak)
    i = i + posun
    if (i > ord(z)):
        i = i - 26
    znak = chr(i)
    zprava = zprava + znak
print("Zašfrovaná zpráva: ", zprava)

So how can I make this code re-useable?

Edit: when I run it in cmd it say: NameError: name ´z´ is not defined

pandik70
  • 185
  • 1
  • 3
  • 9

2 Answers2

0

Perhaps all you need is for your program window to stay open until you can inspect the result. But your question is about continuing to run the program after it reaches the end, so here is the answer to that:

  1. Open a Windows command window ("CMD prompt").

  2. Run your program by explicitly calling the python interpreter, and add the -i flag:

    C:\> python -i program.py
    
  3. After the program runs and reaches the end of the script, you'll get an interactive prompt with access to all your variables. You can now do whatever you want with the environment, e.g., rerun sifra().

An alternative: Open your program with IDLE, python's default editor, and run it in the interpreter window. The effect will be the same.

alexis
  • 48,685
  • 16
  • 101
  • 161
  • I am probably lost... if I write C:\> python -i program.py in cmd it says invalid syntax.... If I open that file in IDLE and hit start module it says RESTART: E:\Users\Mata\Desktop\Programovani\Python\caesarova_sifra.py – pandik70 Jul 31 '16 at 15:47
0

Problem was in type of variable zprava..it should be zprava = "" not zprava = 0 and in missing quotation marks in if (i > ord(z)): --> if (i > ord("z")):

But one thing that is still not working is ending program..Why sys.exit() does not work?

pandik70
  • 185
  • 1
  • 3
  • 9