Python:在while循环中使用if循环

时间:2017-01-30 13:06:01

标签: python

所以我试图制作一个模因生成器(我知道随机的想法,但它是一个Twitch流光)我希望能够使我的输入循环,所以我可以继续要求模因输入。我的代码如下所示:

import time

print("Hey guys this is Alex from Protostar here.")
time.sleep(2)
print("And welcome to the Protostar random meme generator!")
time.sleep(2)
print("This is made by the cheeky scrub known as @dylan_purser on twitter and twitch lol xD")
time.sleep(2)
print("He tries to make memes but does a shit job of them lol still check me out on soundcloud: @dylanpursermusic LUL")
time.sleep(4)
print("Are you ready then?")
time.sleep(2)
print("Lets load the program, BUDDY!!!")
time.sleep(2)

memecount = 0

print("Current memes coded: Dank Memes, Chrissy, WOO, Start, Mate, Skrillex, DAW")
print("PLEASE USE SAME CAPITALISATION AS THE MEMES LIST!")

meme = input("What danke memes do you want?")

while True:
    if meme == "Dank Memes":
        print("danke mem ays man")
        time.sleep(1)

    elif meme == "Chrissy":
        print("Chrissy just like died... RIP Chrissy.exe")
        time.sleep(1)

    elif meme == "WOO":
        print("WOOOH")
        time.sleep(2)
        print("WOOOO")
        time.sleep(2.5)
        print("WOOOOOO")
        time.sleep(1)

    elif meme == "Start":
        print("Start right off with a f*cking meme")
        time.sleep(1)

    elif meme == "Mate":
        print("ALRIGHT MATE!")
        time.sleep(1)

    elif meme == "Skrillex":
        print("I AM SKRILLEX")
        time.sleep(1)

    elif meme == "DAW":
        print("Because I use Reason")
        time.sleep(2)
        print("What software do I use? Read the fucking info mate!")
        time.sleep(2)
        print("You ever heard of a de-esser mate?")
        time.sleep(1)

(代码不会运行btw)

当我运行它时,它只是循环if / elif命令(例如,它只是继续打印" danke mem ays man"当我输入" Dank Memes"。

有没有人能解决我的问题?

1 个答案:

答案 0 :(得分:1)

输入需要嵌套在while循环中,否则循环将永远保持在同一输入上,因为它是while True

尝试:

while True:
    meme = input("What danke memes do you want?")
    if meme == "Dank Memes":
        print("danke mem ays man")
        time.sleep(1)

    elif meme == "Chrissy":
        print("Chrissy just like died... RIP Chrissy.exe")
        time.sleep(1)

    elif meme == "WOO":
        print("WOOOH")
        time.sleep(2)
        print("WOOOO")
        time.sleep(2.5)
        print("WOOOOOO")
        time.sleep(1)

    elif meme == "Start":
        print("Start right off with a f*cking meme")
        time.sleep(1)

    elif meme == "Mate":
        print("ALRIGHT MATE!")
        time.sleep(1)

    elif meme == "Skrillex":
        print("I AM SKRILLEX")
        time.sleep(1)

    elif meme == "DAW":
        print("Because I use Reason")
        time.sleep(2)
        print("What software do I use? Read the fucking info mate!")
        time.sleep(2)
        print("You ever heard of a de-esser mate?")
        time.sleep(1)

..首先:)