代码疑问:clean/count函数未按指定Endpoint执行,需实现重复交互
问题解决与代码修改
1. 限定操作范围(核心修改)
要让clean和count仅处理从行0到endRow、列0到endColumn的区域,只需将遍历范围从整个SIZE改为endRow+1和endColumn+1(Python的range是左闭右开区间,加1才能包含指定的行/列)。
修改后的clean函数:
def clean(world, endRow, endColumn): print("Scooping the poop") # 仅遍历0到endRow的行、0到endColumn的列 for r in range(endRow + 1): for c in range(endColumn + 1): if world[r][c] == POOP: world[r][c] = CLEANED
修改后的count函数:
def count(world, endRow, endColumn): print("Counting number of occurances of a character") number = 0 element = input("Enter character: ") # 仅遍历指定范围内的区域 for r in range(endRow + 1): for c in range(endColumn + 1): if world[r][c] == element: number += 1 return(element, number)
2. 实现重复交互逻辑
在start函数中加入循环,每次完成统计和清理后,询问用户是否继续操作,输入q即可退出。
修改后的start函数:
def start(): world = createWorld() display(world) while True: endRow, endColumn = getEndPoint() element, number = count(world, endRow, endColumn) print("# occurances of %s=%d" % (element, number)) clean(world, endRow, endColumn) display(world) # 询问是否继续操作 choice = input("Enter 'q' to quit, or any other key to continue: ") if choice.lower() == 'q': break start()
3. 修复遗漏的模块导入
原代码中createElement使用了random模块但未导入,需在代码开头添加:
import random
完整修改后的代码
import random SIZE = 4 EMPTY = " " PERSON = "P" PET = "T" POOP = "O" ERROR = "!" CLEANED = "." MAX_RANDOM = 10 def clean(world,endRow,endColumn): print("Scooping the poop") for r in range(endRow + 1): for c in range(endColumn + 1): if world[r][c] == POOP: world[r][c] = CLEANED # @args(reference to a alist of characters,integer,integer) # Function will count the number of occurences of the character specified by the user up the specified # end point (row/column specified by the user). # @return(character,integer) def count(world,endRow,endColumn): print("Counting number of occurances of a character") number = 0 element = input("Enter character: ") for r in range(endRow + 1): for c in range(endColumn + 1): if world[r][c] == element: number += 1 return(element,number) # Randomly generates an 'occupant' for a location in the world # @args(none) # Randomly generates and returns a character according to the specified probabilities. # *50% probability empty space # *20% probability of a person # *10% probability of a pet # *20% probability of fecal matter # @return(character) def createElement(): tempNum = random.randrange(MAX_RANDOM)+1 # 50% chance empty if ((tempNum >= 1) and (tempNum <= 5)): tempElement = EMPTY # 20% of a person elif ((tempNum >= 6) and (tempNum <= 7)): tempElement = PERSON # 10% chance of pet elif (tempNum == 8): tempElement = PET # 20% chance of poop in that location (lots in this world) elif ((tempNum >= 9) and (tempNum <= 10)): tempElement = POOP # In case there's a bug in the random number genrator else: tempElement = ERROR return(tempElement) # Creates the SIZExSIZE world. Randomly populates it with the # return values from function createElement # @args(none) # Randomly populates the 2D list which is returned to the caller. # @return(reference to a list of characters) def createWorld(): world = [] # Create a variable that refers to a 1D list. r = 0 # Outer 'r' loop traverses the rows. # Each iteration of the outer loop creates a new 1D list. while (r < SIZE): tempRow = [] world.append(tempRow) # Create new empty row c = 0 # The inner 'c' loop traverses the columns of the newly # created 1D list creating and initializing each element # to the value returned by createElement() while (c < SIZE): element = createElement() tempRow.append(element) c = c + 1 r = r + 1 return(world) # Shows the elements of the world. All the elements in a row will # appear on the same line. # @args(reference to a list of characters) # Displays the 2D list with each row on a separate line. # @return(nothing) def display(world): print("OUR WORLD") print("========") r = 0 while (r < SIZE): # Each iteration accesses a single row c = 0 while (c < SIZE): # Each iteration accesses an element in a row print(world[r][c], end="") c = c + 1 print() # Done displaying row, move output to the next line r = r + 1 # @args(none) # @return(integer,integer) def getEndPoint(): endRow = int(input("Enter the row (0-3): ")) while not 0 <= endRow <= 3: print("Invalid input. Value must be in range 0-3") endRow = int(input("Enter the row: ")) endColumn = int(input("Enter the column (0-3): ")) while not 0 <= endColumn <= 3: print("Invalid input. Value must be in range 0-3") endColumn = int(input("Enter the column: ")) return(endRow,endColumn) # Starting execution point for the program. def start(): world = createWorld() display(world) while True: endRow, endColumn = getEndPoint() element, number = count(world, endRow, endColumn) print("# occurances of %s=%d" % (element, number)) clean(world, endRow, endColumn) display(world) choice = input("Enter 'q' to quit, or any other key to continue: ") if choice.lower() == 'q': break start()
内容的提问来源于stack exchange,提问作者anonymous
相关产品推荐
相关产品推荐

