-2

how can I shut down script in python? sys.exit() does not work.. so where the problem is? Edit: changed indentation as in my file import sys def sifra(): retezec = input("Zadejte slovo: ") print("Zadali jste slovo: ",retezec) zprava = "" 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šifrovaná zpráva: ", zprava)

    znovu = input("Znovu? A/N ")
    if(znovu == "A" or "a"):
        sifra()
    elif(znovu == "N" or "n"):
        sys.exit()
    else:
        pass
sifra()
pandik70
  • 185
  • 1
  • 3
  • 9

1 Answers1

0

znovu == "N" or "n" is not what you want. You need to compare znovu to both these values:

elif (znovu == 'N') or (znovu == 'n'):
    sys.exit()

Another alternative would be the one suggested in the comments or simply:

elif znovu in 'Nn': sys.exit()

Assuming that nobody inputs 'Nn' as an answer, but even if they (accidentally) do so, this can still be considered as 'No'.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    Alternatively: ```elif znovu in ['N', 'n']:``` – Owen Aug 01 '16 at 11:21
  • If I use `elif (znovu == 'N') or (znovu == 'n'): sys.exit()` program immediately closes and in cmd error is "unindent does not match any outer indentation level" and if I corret the `if(znovu == "A" or "a"): sifra()` to `if(znovu == 'A') or (znovu == 'a'): sifra()` the error is same – pandik70 Aug 01 '16 at 15:34
  • @pandik70, make sure you're not mixing tabs and spaces in your code – ForceBru Aug 01 '16 at 15:35
  • @ForceBru do you know any way to check it out? – pandik70 Aug 01 '16 at 15:40
  • @pandik70, well, if your code isn't too huge, you can create a new file and rewrite it there (don't copy and paste!). You could also write a new Python script that reads a file and outputs its contents through the `repr` function and make it run through your code: it will output `\\t` if it finds a tab and a blank space if it finds a space. Then check whether there are mixed spaces & tabs and edit these mixes away. – ForceBru Aug 01 '16 at 15:45
  • @ForceBru Done... [this is the code...](http://paste.ofcode.org/gxY69rkVZeTpxDHjxuUYHf) I didnt use multiple spaces instead of tab.. Does space between if and bracket matter? – pandik70 Aug 01 '16 at 16:01
  • @pandik70, first, you have to add `:` after function declaration. Second, Python's code blocks are separated by alignment, or, to be precise, indentation (we use curly brackets in C/C++ and `BEGIN`/`END` in Pascal for this). Now, you can see that `retezec` in the first line of the function is misplaced: it sorta 'pops out' of the overall structure (in the link you posted it can be seen clearly). This is a problem. – ForceBru Aug 01 '16 at 16:20
  • Here you've used four spaces instead of one Tab symbol. Simply get rid of the spaces and insert a tab instead. – ForceBru Aug 01 '16 at 16:21
  • @pandik70, [This works fine](http://pastebin.com/T0ZwMBFC) (includes some code-style tips widely used among Python programmers) – ForceBru Aug 01 '16 at 16:31