我正在参加一个介绍comp sci课程,目前的项目是递归的.该程序读取一个文本文件,该文件有n行和n列的-
和I
.该程序创建一个2D列表,该列表是用于该程序的网格.然后提示用户在网格上的一个点(row,col),如果用户的点是a,则-
该点变为a @
然后我必须@
通过检查上面的空格,左边递归地填充第一个可用空间,右边,下方并填充第一个可用空间.我的递归函数是:
def fillWithAt(grid, the_row, the_col):
if grid[the_row][the_col] == '-':
grid[the_row][the_col] = '@'
printFile(grid)
if the_row != 0:
if grid[the_row - 1][the_col] == '-':
fillWithAt(grid, the_row - 1, the_col)
if the_col != 0:
if grid[the_row][the_col - 1] == '-':
fillWithAt(grid, the_row, the_col - 1)
if the_col != (len(grid[the_row]) - 1):
if grid[the_row][the_col + 1] == '-':
fillWithAt(grid, the_row, the_col + 1)
if the_row != (len(grid) - 1):
if grid[the_row + …
Run Code Online (Sandbox Code Playgroud) 我正在研究一个需要我从用户那里获取浮点值的类项目.此浮点值必须在十进制后正好有两个数字才能成为有效输入.这就是我到目前为止所拥有的.
while True:
try:
cost = float(input("Enter the price: "))
if cost % 1 == 0:
print("Invalid input for price.")
else:
if cost > 0:
return cost
except ValueError:
print("Invalid input for price.")
Run Code Online (Sandbox Code Playgroud)
相比cost % 1
于0
排除了整数和.00花车结束,但我不知道我怎么会小数(iexxx)后,限制接受输入到浮动恰好与2号.另外我相信我需要接受像5.00一样的浮动,所以我的方法不会削减它.我已经尝试过转换cost
为str并设置长度限制,但这仍然让它容易出错.有什么建议?