简体   繁体   English

如果没有定义变量的语句(Python)

[英]If statements without defining variables (Python)

How would I go about writing a simple set of nested if statements that check the user input and don't require variables ?我将如何 go 关于编写一组简单的嵌套 if 语句来检查用户输入并且不需要变量

For example ( pseudo-code )例如(伪代码

If userinput == "A":
    print("great, next?")
    if userinput == "A"
       print("great, your second letter is A, again")
       print("what's next?")
       if userinput == "A"
           print("you have a lot of creativity")
       elif userinput == "B"
           print("so, so far we have A, A, B, interesting")
       elif userinput == "C"
           print("A C!")
    elif userinput = "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'")

I want this to be able to continue dozens of levels deep.我希望这能够继续深入数十个级别。

Edit: I've corrected the pc with elif, thanks for the suggestion.编辑:我已经用 elif 更正了电脑,谢谢你的建议。

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:将其保存到一个名为bad_dont_do_this.py的文件中,然后像这样运行它:

$ python bad_dont_do_this.py

and type in your A's, B's, and C's.并输入您的 A、B 和 C。

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:编辑:这是我按照指示得到的 output:

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.请注意x如何绑定到 if 语句的每个分支中的新用户输入。 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.就像另一位评论者提到的那样,如果您能够使用足够新的 Python 版本,您可以使用海象 ( := ) 和匹配功能使其更加简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM