2

下面的代码是提供给我们的,它基本上打印了第二个函数中显示的图像:

import sys



# CONSTANTS
MIN_ROW = 0
MAX_ROW = 9
MIN_COLUMN = 0
MAX_COLUMN = 9
WALL = "#"
BUILDING = "b"
BUSH = "u"
PLAYER = "@"
EMPTY = " "
STAIRS = "X"


def display (city):
   r = 0
   c = 0
   print("CITY LEVEL")
   for r in range (0, (MAX_ROW+1), 1):   #LOOPS1
      for c in range (0, (MAX_COLUMN+1), 1):
         sys.stdout.write(city[r][c])
      print()
   print()

def initialize ():
   r = 0
   c = 0
   city = []

   for r in range (0, (MAX_ROW+1), 1): #LOOP2
      city.append([])#appends an empty list to city
      for c in range (0, (MAX_COLUMN+1), 1):
       city[r].append(" ")
   #               0   1   2   3   4   5   6   7   8   9 
   city  [0] =   ["#","#","#","#","#","#","#","#","#","#"]
   city  [1] =   ["#","@"," "," "," "," "," "," ","u","#"]
   city  [2] =   ["#"," "," ","b","b"," "," "," ","X","#"]
   city  [3] =   ["#"," "," ","b","b"," "," "," "," ","#"]
   city  [4] =   ["#"," "," "," "," "," "," "," ","b","#"]
   city  [5] =   ["#","u"," ","u","u","u","u","u","u","#"]
   city  [6] =   ["#","b"," "," "," "," "," "," "," ","#"]
   city  [7] =   ["#"," "," "," "," ","b"," ","b"," ","#"]
   city  [8] =   ["#"," "," "," ","b"," "," "," "," ","#"]
   city  [9] =   ["#","#","#","#","#","#","#","#","#","#"]
   return city   


# MAIN
def main ():
     level = initialize ()
     display (level)





main ()

现在我试图为一维图片重现这个,但由于某种原因,我遇到了第一个函数的 sys.stdout.write() 的类型错误。与仅其中一个字符相比,它似乎试图打印整个列表。任何人都可以帮我调试吗?也有人可以告诉我上面标记为 LOOPS1 和 LOOPS2 的代码中的循环中发生了什么

import sys


def display(track):
    c=0
    for c in range(0,20,1):
        sys.stdout.write(track[c])
    print()

def initialize():
    c=0
    track= []
    for c in range(0,20,1):
        track.append([])
        track[c].append(" ")
    track[0]= ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"]
    return track

level= initialize()
display(level)

如果有任何不清楚的地方,请告诉我,我会尽快解决。

编辑:我的程序代码:

import sys
import random


# CONSTANTS


PLAYER = "@"
EMPTY = " "


#Takes the information from the function initialize() and displays it. Outputs the fitness simulation.
def display (track):
    r = 0
    c = 0

    print("\nTRACK")
    for r in range (0, (4), 1):
        for c in range (0, (41), 1):
            sys.stdout.write(track[r][c])
        print()
    print()

def speedDisplay(speed):
    options=["(w)alk","(j)og","(r)un","(f)ast run"]
    for o in range(0,speed,1):
        print(options[o],"\n")




def inputs():#ioerror here?
    values= set("wWjJrRfFlLsSeE")
    while True:
        move=input("\nPlease select the speed you would like to travel at from the options listed:")
        for m in move:
            if m not in values:
                print("\nInadmissable entry, Please only use inputs valid in the options above.")
                break
        else:
            break
    if move=="w" or move=="W":
        usedEnergy=0#turn into random functions later
    elif move=="j" or move=="J":
        usedEnergy=1
    elif move=="r" or move=="R":
        usedEnergy=2
    elif move=="f" or move=="F":
        usedEnergy=5
    return usedEnergy

def remainingEnergy(energy,usedEnergy):
    energy= energy-usedEnergy
    print("\nRemaining Energy:",energy,"\n")
    return energy

def amountLeft(energy):
# enter ioexception error here somewhere?


    while True: 
        if energy <0 or energy >20:
            print("error")
        elif energy>=5:
            speed=4
        elif energy <5 and energy >=2:
            speed=3
        elif energy <2 and energy >=1:
            speed=2
        elif energy <1 and energy >=0:
            speed=1
        else:
            break

        return speed











# This function is used to initialize the game track that will later be displayed. 
def initialize ():
    r = 0
    c = 0
    track = []
    #Creates each row and column. A "for" loop initiates which creates and appends an empty list to the list "track". Then, taking the current row into consideration, the respective number of columns are created via the inner "for loop and a space is appended to the end of the current row. The loop re-initiates and the process is repeated for all 4 required rows. This results in 4 rows and 41 coloumns.
    for r in range (0, (4), 1):
    #appends an empty list to track
        track.append([])
        for c in range (0, (41), 1):
    #appends a space to the current row
            track[r].append(" ")
    # the actual rows and columns are created below.
    #               0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y
    track  [0] =   [" ","0"," ","1"," ","2"," ","3"," ","4"," ","5"," ","6"," ","7"," ","8"," ","9"," ","A"," ","B"," ","C"," ","D"," ","E"," ","F"," ","G"," ","H"," ","I"," ","J"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "]
    track  [1] =   [" ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," "]
    track  [2] =   ["|","@","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"]
    track  [3] =   [" ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," "]
    return track   






def move (sRow, sCol, dRow, dCol, track):
    EMPTY= " "
    PLAYER= "@"
    DIVIDER= "|"
    track[sRow][sCol] = EMPTY
    track[dRow][dCol] = PLAYER






# MAIN
def main ():
    track = initialize ()
    display (track)
    print("\n(w)alk\n\n(j)og\n\n(r)un\n\n(f)ast run")
    usedEnergy=inputs()
    energy=20
    energy=remainingEnergy(energy,usedEnergy)
    while energy<20:
        usedEnergy=inputs()
        speed= amountLeft(energy)
        speedDisplay(speed)
        energy=remainingEnergy(energy,usedEnergy)



main ()
4

1 回答 1

2

好吧,这行得通:

import sys

def display(track):
    c=0
    for c in range(0,20,1):
        sys.stdout.write(track[c])
    print()

def initialize():
    c=0
    track = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"]
    return track

level= initialize()
display(level)

您的问题是您的处理方式不匹配track。在initialize您将它创建为列表列表时,但在display您访问它时,就像它只是一个字符列表一样。制作两个字符列表更简单,制作两个字符列表更通用(允许将相同的函数用于 1D 和 2D 案例)。

第一个循环发生了什么?

display您的第一个代码块中:

for r in range (0, (MAX_ROW+1), 1):   #LOOPS1
     for c in range (0, (MAX_COLUMN+1), 1):
         sys.stdout.write(city[r][c])
     print()
 print()

R 是从 0 到 MAX_ROW,0 - 9 变化的行索引。C 是从 0 到 MAX_COLUMN,0 - 9 变化的列索引。

sys.stdout.write(city[r][c])在没有换行符的情况下写出 city 的当前行和列(已设置为单个字符)。

由于 print() 语句,您的代码可能是 Python 3。如果您提到或标记它可能会有所帮助。

我正在运行 Python 2.7,因此我将使用旧的打印语法,以便测试我发布的代码。您可能必须将其还原(添加括号)。

我会这样写:

def display (city):
   print("CITY LEVEL")
   for row in city:
      for c in row:
         print c,
      print
   print

在 Python 2 中,打印末尾的逗号会抑制换行符。

第二个循环发生了什么?

initialize您的第一个代码块中:

for r in range (0, (MAX_ROW+1), 1):

r将是 0 到 MAX_ROW,0-9

    city.append([])#appends an empty list to city

city是一个列表。这会将一个空列表附加到城市。

    for c in range (0, (MAX_COLUMN+1), 1):

c将 b 0 到 MAX_COLUMN, 0-9

        city[r].append(" ")

将空格字符附加到当前行。

我会这样写:

def initialize():
   city = [
       # 0   1   2   3   4   5   6   7   8   9
       ["#","#","#","#","#","#","#","#","#","#"], # 0
       ["#","@"," "," "," "," "," "," ","u","#"], # 1
       ["#"," "," ","b","b"," "," "," ","X","#"], # 2
       ["#"," "," ","b","b"," "," "," "," ","#"], # 3
       ["#"," "," "," "," "," "," "," ","b","#"], # 4
       ["#","u"," ","u","u","u","u","u","u","#"], # 5
       ["#","b"," "," "," "," "," "," "," ","#"], # 6
       ["#"," "," "," "," ","b"," ","b"," ","#"], # 7
       ["#"," "," "," ","b"," "," "," "," ","#"], # 8
       ["#","#","#","#","#","#","#","#","#","#"], # 9
   ]
   return city

关于 city.append([])

initialize有一行(在第一个循环for中):

city.append([])#appends an empty list to city

在进入循环之前city是:

[]

在第一个附加之后city是:

[[]]

那是一个包含一个项目的列表,一个空列表。

如果该行是:

city.append(0)

这将是:

[0]

或者:

city.append("Jim")

它会给你:

["Jim"]

但是我们在列表中粘贴另一个列表,创建一个列表列表。

我希望这有帮助。

于 2012-11-21T06:32:58.963 回答