簡體   English   中英

Python遞歸錯誤,但不使用遞歸

[英]Python recursion error, but not using recursion

我沒有在代碼中使用遞歸,所以我不明白為什么我遇到關於遞歸的錯誤。 ................................................... ...................................................

代碼是:

from os import system
from time import sleep
import msvcrt 

player_location = [0, 0]
player_dir = [0, 0]
width = int(raw_input('Width:'))
height = int(raw_input('Height:'))

def get_input():
    if msvcrt.kbhit():
        return msvcrt.getch()
    else:
        return ''

def update_screen():
    global player_location
    system('cls')
    for i in range(0, (height-1)):
        if i == player_location[0]:
            print (' ' * (player_location[1] - 1)) + 'X' + (' '*(width*2 - player_location[1] - 1)) + '|'
        else:
            print (' ' * width * 2) + '|'
    print ('-' * width * 2) + '+'

 def get_input():
    global player_dir
    player_dir = [0, 0]
    inp = get_input()
    if inp == 'q':
        exit()
    elif inp == 'w':
        player_dir[0] = -1
    elif inp == 's':
        player_dir[0] = 1
    elif inp == 'd':
        player_dir[1] = 1
    elif inp == 'a':
        player_dir[1] = -1

def actions():
    global player_dir, player_location
    player_location[0] += player_dir[0]
    player_location[1] += player_dir[1]

if __name__ == '__main__':
    while True:
        update_screen()
        sleep(10)
        get_input()
        actions()

錯誤是這樣的:

Traceback (most recent call last):

然后這行很多次:

  File "C:\Python\txtpltfrmr.py", line 29, in get_input
    inp = get_input()

然后這行:

RuntimeError: maximum recursion depth exceeded
def get_input():
    if msvcrt.kbhit():
        return msvcrt.getch()
    else:
        return ''

def get_input():
    global player_dir
    player_dir = [0, 0]
    inp = get_input() # INFINITE RECURSION, CALLS ITSELF

有你的問題。 您有兩個具有匹配簽名的函數,稱為get_input 重命名其中一個,可能是第一個重命名為get_character

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM