Biggus Dickus
Biggus Dickus

Reputation: 23

Beginner Python problem with if statement

Apologies in advance because this is probably fairly stupid.

Basically I was just messing around trying to figure out how to work with inputs (I'm pretty new to coding) and I came across an issue within an if statement.

x=input("What is your name? ")
name=x
print("Hello,", name, "nice to meet you")
y=input("Would you like me to close now? (yes/no) ")
listx=["no","No","NO","nO"]
for response in listx:
    if response == y:
        input("Why? ")
        input("I think you're being rather terse", name, ". ")
        input("No, you. ")
        input("So that's how it's going to be? ")
        input("Well I'm closing anyway. ")
        input("Bye then. ")

I kind of just thought it would take me through this funny little exchange and over time I could customise the responses but there's a problem at this point:

input("I think you're being rather terse", name, ". ")

At this juncture the code doesn't seem to recognise name; I defined name because it wasn't working when I just used x either. I'm assuming that the code doesn't recognise it because the if statement is essentially within a vacuum or something but how exactly would I work around this? I want to be able to call back details which happened previously within the conversation to try to make it funnier.

Upvotes: 2

Views: 48

Answers (1)

Green Cloak Guy
Green Cloak Guy

Reputation: 24711

You can't do that with input() statements - it only works with print(). So print() can take any number of strings as an argument, but input() will only take one (and in recent versions of python will raise an error if you try to give it more than one - when I tried to run your code myself, I got a TypeError).

If you want to include name in the text of the input(), you'll need to either concatenate it:

input("I think you're being rather terse " + name + ". ")

or use a format string to insert it:

input(f"I think you're being rather terse {name}. ")

Upvotes: 1

Related Questions