如何在 if 语句中调用函数?

时间:2021-03-19 18:09:23

标签: python

在艾哈迈德·比尔金的帮助下

CODE

PYTHON SHELL


CODE

PYTHON SHELL

我试图在 if 语句中调用 def 函数,但是当我这样做时,它不起作用,只会返回到 python shell。

我不知道该怎么办我已经尝试了一些我知道的东西并在谷歌上搜索,但似乎找不到任何东西。

def main2():
    print("Vul hieronder de tekst die je wilt ontsleutelen.")
    dec_word = input("> ")
    print("Vul hieronder de cijfer van de verschuiving. (Van 1 tot 25!)")
    dec_keys = int(input("> "))
    allowed_numbers = list(range(1, 26))
    while dec_keys not in allowed_numbers:
        print("Foute Optie!")
        dec_keys = int(input("> "))

    dec_decrypted = decrypt(dec_keys,dec_word)
    print("Ontsleuteld:"+ dec_decrypted +".")
    print("Ontsleuteling van:"+ dec_word +".")
    print("Verschuiving: "+ str(dec_keys) +".")
    input("prompt:")

def main():
    print("Vul hieronder de tekst die je wilt versleutelen.")
    word = input("> ")
    print("Vul hieronder de cijfer van de verschuiving. (Van 1 tot 25!)")
    allowed_numbers = list(range(1, 26))
    keys = int(input("> "))
    while keys not in allowed_numbers:
        print("Foute Optie!")
        keys = int(input("> "))

    encrypted = encrypt(keys,word)
    print("Versleuteling: "+ encrypted +".")
    decrypted = decrypt(keys,encrypted)
    print("Versleuteld van: "+ decrypted +".")
    print("Verschuiving: "+ str(keys) +".")
    input("prompt:")


print("Wil je een woord ontsleutelen of versleutelen")
choice1 = input("> ")
allowed_choices = ["ontsleutelen", "versleutelen"]
while choice1.lower() not in allowed_choices:
print("Foute Optie!")
    choice1 = input("> ")
    if choice1.lower() == ("versleutelen"):
        main()
    if choice1.lower() == ("ontsleutelen"):
        main2()

问题是当我为choice1.lower() 输入“versleutelen”和“ontsleutelen”时,它不会继续 main() 或 main2()

1 个答案:

答案 0 :(得分:0)

所以我也有组织的解决方案:

尝试将第 74 行替换为:choice1 = ""

如果您注意到了,您是在 while 循环之前请求另一个输入。然后你进入循环。尝试删除第 74 行并为选择分配不同的值。因为即使你输入正确的单词,它也会在while循环中误解它,因为它在列表中。如果删除第 74 行,还不够,因为choice1.lower() not in allowed_choices: 会显示错误choice was not found。

def main2():
    print("Vul hieronder de tekst die je wilt ontsleutelen.")
    dec_word = input("> ")
    print("Vul hieronder de cijfer van de verschuiving. (Van 1 tot 25!)")
    dec_keys = int(input("> "))
    allowed_numbers = list(range(1, 26))
    while dec_keys not in allowed_numbers:
        print("Foute Optie!")
        dec_keys = int(input("> "))

    dec_decrypted = decrypt(dec_keys,dec_word)
    print("Ontsleuteld:"+ dec_decrypted +".")
    print("Ontsleuteling van:"+ dec_word +".")
    print("Verschuiving: "+ str(dec_keys) +".")
    input("prompt:")

def main():
    print("Vul hieronder de tekst die je wilt versleutelen.")
    word = input("> ")
    print("Vul hieronder de cijfer van de verschuiving. (Van 1 tot 25!)")
    allowed_numbers = list(range(1, 26))
    keys = int(input("> "))
    while keys not in allowed_numbers:
        print("Foute Optie!")
        keys = int(input("> "))

    encrypted = encrypt(keys,word)
    print("Versleuteling: "+ encrypted +".")
    decrypted = decrypt(keys,encrypted)
    print("Versleuteld van: "+ decrypted +".")
    print("Verschuiving: "+ str(keys) +".")
    input("prompt:")


print("Wil je een woord ontsleutelen of versleutelen")
choice1 = ""
allowed_choices = ["ontsleutelen", "versleutelen"]
while choice1.lower() not in allowed_choices:
    choice1 = input("> ")
    if choice1.lower() == ("versleutelen"):
        main()
    if choice1.lower() == ("ontsleutelen"):
        main2()
相关问题