ZarryMyles
ZarryMyles

Reputation: 23

I have trouble with Python outputting "None" with every line of output

I am trying to make python cipher and decipher text with the Playfair method. But I hit a roadblock because it seems to output "None" with every line out output. I'd be grateful if anyone tells me why it's doing so. (This is my first post, so please bear with any mistakes I might have made). My code:

def cip():
    key=input(print("Please Enter Keyword: "))
    return key

def inp():
    c = int(input(print("1.Cipher text \n2.Exit\n\t>>")))
    if c==1:
        cip()
    else:
        exit
inp()

Output:

C:\Users\XYZ\Desktop\Code\Py programs>python -u "c:\Users\XYZ\Desktop\Code\Py programs\Playfair.py"
1.Cipher text
2.Exit
        >>
None1
Please Enter Keyword:
NoneTron

Upvotes: 0

Views: 52

Answers (2)

mprostak
mprostak

Reputation: 246

The problem is when you use input with print in it. Print should be outside.

def cip():
    print("Please Enter Keyword: ")
    key=input()
    return key

def inp():

    print("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>")
    c = int(input())
    if c==1:
        cip()
    elif c==2:
        decip()
    else:
        exit

inp()

You could also put the string in input(), like this:

c = int(input("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>"))

Upvotes: 1

Barmar
Barmar

Reputation: 780724

The problem is your use of print() in the input() call.

c = int(input(print("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>")))
              ^^^^^

print() prints its argument, and returns None. input() uses the value of its argument as the prompt, so it's printing None as the prompt.

Just pass a prompt string to input(), don't call print()

c = int(input("1.Cipher text \n2.De-cipher text\n3.Exit\n\t>>"))

Upvotes: 2

Related Questions