If statements without defining variables (Python)
Solution 1:
To answer the exact question you asked, where you want to prompt the user for input in a way that "doesn't require variables" (by which I assume you mean that you don't want to bind any values to variable names):
{"A": lambda: (
print("great, next?"),
{"A": lambda: (
print("great, your second letter is A, again"),
print("what's next?"),
{"A": lambda:
print("you have a lot of creativity"),
"B": lambda: print("so, so far we have A, A, B, interesting"),
"C": lambda: print("A C!")
}.get(input(), lambda: None)()),
"B": lambda: print("love the choice of B"),
}.get(input(), lambda: print("I can't accept that, you needed to type 'A' or 'B'"))()
),
"B": None
}.get(input(), lambda: print("you needed to type 'A'"))()
Save that to a file called bad_dont_do_this.py
and then run it like:
$ python bad_dont_do_this.py
and type in your A's, B's, and C's.
Unless you're trying to win a bet or something, this is not good code and you shouldn't use this approach. I'd be interested to hear why you can't use variables in your code. You almost certainly want to ask another question where you're more clear about the higher level goals you're trying to achieve.
Edit: this is the output I get when I follow my directions:
C:\>python bad_dont_do_this.py
A
great, next?
A
great, your second letter is A, again
what's next?
A
you have a lot of creativity
C:\>python bad_dont_do_this.py
A
great, next?
A
great, your second letter is A, again
what's next?
B
so, so far we have A, A, B, interesting
C:\>python bad_dont_do_this.py
A
great, next?
B
love the choice of B
C:\>python bad_dont_do_this.py
Z
you needed to type 'A'
Based on your comment about not wanting to create new variable names for all of the nested conditions, you can consider doing something like the following:
x = input("> ")
if x == "A":
print("great, next?")
x = input("> ")
if x == "A":
print("great, your second letter is A, again")
print("what's next?")
x = input("> ")
if x == "A":
print("you have a lot of creativity")
elif x == "B":
print("so, so far we have A, A, B, interesting")
elif x == "C":
print("A C!")
elif x == "B":
print("love the choice of B")
else:
print("I can't accept that, you needed to type 'A' or 'B'")
else:
print("you needed to type 'A'")
Note how x
gets bound to the new user input in each branch of the if statements. Like another commenter mentioned, if you are able to use a new-enough Python version, you can probably make this even more succinct with the walrus (:=
) and match features.