skip to Main Content
name = input("Who are you? ")
print("Are you here to test me?")
if input("y"):
    print("I'll banish you " + name + ", out of this realm")
if input("n"):
    print("Good. Now leave.")

I used Python in Visual Studio Code.

When executed, after I enter my name, the terminal literally says "y". I don’t want it to literally say "y". When I type anything else, the code still prints! I only want it to print when I type ‘y’ or ‘n’. Also, it’ll execute the code in the order given, not with the corresponding letter (so if I say n, it’ll give the results of y.) Please I am a beginner and I want to at least know what’s up so I can learn from my mistakes.

4

Answers


  1. Here’s the corrected code –

    name = input("Who are you? ")
    print("Are you here to test me?")
    
    answer = input() # assign the user's input to a variable
    
    if answer == "y": # compare the variable against the desired values
        print("I'll banish you " + name + ", out of this realm")
    
    if answer == "n":
        print("Good. Now leave.")
    
    Login or Signup to reply.
  2. name = input("Who are you? ")
    answer =input("Are you here to test me? ").lower()
    if answer == 'y':
        print("I'll banish you " + name + ", out of this realm")
    elif answer == 'n':
        print("Good. Now leave.")
    

    Some outputs:

    Who are you? Aj
    Are you here to test me? y
    I'll banish you Aj, out of this realm
    
    Who are you? Aj
    Are you here to test me? Y
    I'll banish you Aj, out of this realm
    
    Who are you? Aj
    Are you here to test me? n
    I'll banish you Aj, out of this realm
    
    Who are you? Aj
    Are you here to test me? N
    I'll banish you Aj, out of this realm
    
    Login or Signup to reply.
  3. The input() function takes a single parameter, which is the prompt to show to the user. If you want to check the users input, you need to compare against the value returned by the input() function.
    You will also want to consider what happens if the user enters a response in a different case, or of a value you don’t expect.

    In this example, the response to the second prompt is immediately converted to lowercase, and then we take only the first character to use for the comparison. I’ve also added a branch to handle if the user input isn’t ‘y’ or ‘n’.

    name = input("Who are you? ")
    
    # Just use input for this prompt instead of a print command
    # .lower() changes the string returned by input to lowercase
    # strings are indexed like arrays, so [0] will return the first character
    answer = input("Are you here to test me? ").lower()[0] 
    
    if answer == "y":
        print("I'll banish you " + name + ", out of this realm")
    elif answer == "n":
        print("Good. Now leave.")
    else: #Answer wasn't yes or no.
        print("I can't understand you.")
    
    Who are you? Bob
    Are you here to test me? Yes
    I'll banish you Bob, out of this realm
    
    Who are you? Bob
    Are you here to test me? no
    Good. Now leave.
    
    Who are you? Bob
    Are you here to test me? Maybe
    I can't understand you.
    
    Who are you? Bob
    Are you here to test me? 12345
    I can't understand you.
    
    Login or Signup to reply.
  4. name = input("Who are you? ")
    myinp = input("Are you here to test me?")
    if myinp.startswith("y"):
        print("I'll banish you " + name + ", out of this realm")
    else:
        print("Good. Now leave.")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search