此程序应根据用户通过rows
方法指定的asterisks
和input()
的数量打印星号矩形。下面是我目前使用的非工作代码:
numRows = input('Please enter the number of rows: ')
numRows = eval(numRows)
numAst = input('Please enter the number of asterisks in a row: ')
numAst = eval(numAst)
for i in range(numRows):
print(numAst*'*')
答案 0 :(得分:2)
我怀疑你是在Python2下运行程序
这会给你
TypeError: eval() arg 1 must be a string or code object
尝试使用Python3
运行注意:像这样使用eval
是危险的。为什么不使用int
?
如果您需要Python2版本。将input
替换为raw_input
,将eval
替换为int
numRows = raw_input('Please enter the number of rows: ')
numRows = int(numRows)
numAst = raw_input('Please enter the number of asterisks in a row: ')
numAst = int(numAst)
for i in range(numRows):
print(numAst*'*')
答案 1 :(得分:0)
它在我的机器上运行,确保你在eval中传递字符串。 点击这里http://docs.python.org/library/functions.html#eval