在Python中添加局部变量(整数)

时间:2014-09-24 08:30:51

标签: python variables python-3.x

我正在尝试创建一个函数,如果用户选择的选项将超过选项输出的两倍,那么当他们选择“bench”选项超过两次时它会改变并给出不同的答案。 Btw - delayedPrint()是我制作的一个逐个打印字符的函数。

def menuChoice():
    notepad = 1
    delayedPrint('\n')
    choice = input('> ')
    if choice == "bench" and notepad < 2:
        moreDelayedPrint("You grab the notepad off the bench." + '\n')
        moreDelayedPrint("You flick through the pages, but don't notice anything written down." + '\n')
        notepad + 1
        optionMenu()
    elif choice == "bench" and notepad > 2:
        moreDelayedPrint("You grab the notepad off the bench." + '\n')
        moreDelayedPrint("Aha! This time you notice a small scribble near the back of the     notepad." + '\n')
        moreDelayedPrint("It reads "3472" + '\n')
        optionMenu()        

无论我选择多少次“替补”选项,它都会显示“你浏览页面,但没有注意到任何记录” - 我的'记事本+ 1'似乎没有用。有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

notepad + 1 返回新值。将其分配回notepad以替换旧的:

notepad = notepad + 1

您还需要为您的函数添加循环;你只是问用户一次:

def menuChoice():
    notepad = 1
    delayedPrint('\n')
    while True:
        choice = input('> ')
        if choice == "bench" and notepad < 2:
            moreDelayedPrint("You grab the notepad off the bench." + '\n')
            moreDelayedPrint("You flick through the pages, but don't notice anything written down." + '\n')
            notepad = notepad + 1
            optionMenu()
        elif choice == "bench" and notepad >= 2:
            moreDelayedPrint("You grab the notepad off the bench." + '\n')
            moreDelayedPrint("Aha! This time you notice a small scribble near the back of the     notepad." + '\n')
            moreDelayedPrint('It reads "3472\n')
            optionMenu()        

现在当optionMenu()返回循环时,会返回choice提示符。