函数返回"无" (不使用代码中的任何花哨功能)

时间:2016-10-22 22:07:03

标签: python python-3.x loops output

我正在使用循环编写游戏,但我还没有在我的代码中定义任何函数,但只使用了print()之类的基本函数。我的代码本身正是它应该做的事情,但它会抛出一个None,用户应该在这里输入代码。

while loca == 22:
    if (lever=="back") and (dial=="red"):
        print("congratlations! You have won the game!")
        play=input(print("Would you like to play again? Type 'Y' for yes and 'N' for no."))
        while play in ["yes","y","Y","Yes"]:
            loca = 1
            play="reset"
        while play in ["no","n","N","No"]:
            print("Thanks for playing!")
            quit()
        while (not play == "reset"):
            while (not play == ["no","n","N","No"]) and (not play == ["yes","y","Y","Yes"]):
                play=input(print("Sorry, I didn't understand. Please enter 'Y' for yes, or 'N' for no."))
    else:
        print("Hmm.. You don't quite have the right combination yet!")      

我知道quit()是一个函数,但我也尝试删除它的代码,它仍然返回None。我从来没有遇到过这个问题,我写的其他程序使用了我在这段代码中使用的相同功能。

对于这个赋值,我们应该在代码中实现退出函数,或者它的一些变体但是我们还没有学习return函数(这段代码不是假设的)有任何作者定义的功能)似乎我在网上找到的唯一答案是return。我以为我会在这里试试运气,也许这只是我想念的小事。

2 个答案:

答案 0 :(得分:3)

print函数返回None,然后传递给input,删除对print的调用,因为input已经占用打印传递的字符串。

而不是:

play=input(print("Would you like to play again? Type 'Y' for yes and 'N' for no."))

使用:

play=input("Would you like to play again? Type 'Y' for yes and 'N' for no.")

答案 1 :(得分:0)

您创建变量play的行。您滥用输入功能,您已在该功能中显示的提示已显示给用户。但是,通过输入print(),您将不返回任何内容。

那一行应该是:

play = input("Would you like to play again? Type 'Y' for yes and 'N' for no.")

P.S。你应该尝试遵守PEP 8 - 蟒蛇风格指南