无法根据用户输入输出形状

时间:2012-07-04 02:44:15

标签: python

此程序应根据用户通过rows方法指定的asterisksinput()的数量打印星号矩形。下面是我目前使用的非工作代码:

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*'*')

2 个答案:

答案 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