2

以下のコードは私たちに提供されたもので、基本的には 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 ()

これを 1D 画像で再現しようとしていますが、何らかの理由で最初の関数の sys.stdout.write() で型エラーが発生しています。リストの 1 文字と比較して、リスト全体を印刷しようとしているようです。誰でもデバッグを手伝ってもらえますか?. また、LOOPS1 AND 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