简体   繁体   English

如何在python 3.2上格式化输入

[英]how to format inputs on python 3.2

while True: 

    letter,Text,numRails= input("").split('\"')
    if numRails ==(""):
        numRails=(2)
        plainText= Text.strip()
    numRails=int(numRails)


    if letter.strip() == "E" or "e":
        x =Encrypt(numRails,Text)

    elif letter.strip()=="D" or "d":
        x =scramble2Decrypt(numRails,Text)

    else: print("invalid encryption command")

E "WEAREDISCOVEREDGO" 2 I need to be able to input all 3 variables on the same line. E“WEAREDISCOVEREDGO”2我需要能够在同一行输入所有3个变量。 however I want to have some conditional statements. 但是我想要一些条件陈述。 but I don't know how to do that. 但我不知道该怎么做。 like do I just do it as you would normally do if it was just one variable or there's a specific way you have to do this ? 就像我通常做的那样,如果它只是一个变量,或者你必须采取特定的方式吗?

Note if conditions are wrong! 注意条件是否错误! it always evaluates to True: 它总是评估为True:

 letter.strip() == "E" or "e":
 #                      ^^^^ always True  

should be: 应该:

 letter.strip() in ["E", "e"]:

Similar mistake is present in second if code. 第二个if代码中存在类似的错误。

Note: a Boolean equivalent of a non-empty string is always True eg 注意:非空字符串的布尔等效值始终为True,例如

>>> bool("") # bool of empty string 
False
>>> bool(" ") # bool of non-empty string 
True
>>> bool("e")
True

So for this reason your if condition eg letter.strip() == "E" or "e": == letter.strip() == "E" or True: == Ture 所以出于这个原因你的if条件,例如letter.strip() == "E" or "e": == letter.strip() == "E" or True: == Ture

As @ roippi is suggesting in comment, you can further simplify if conditional expressions just as follows: 正如@ roippi在评论中建议的那样,如果条件表达式如下,则可以进一步简化:

letter.strip().lower() == 'e':

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

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