无法根据用户inpu输出形状

2024-09-30 05:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

这个程序应该根据用户通过input()方法指定的rows和{}的数量打印一个由星号组成的矩形。以下是我当前使用的非工作代码:

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

Tags: ofthe方法用户in程序numberinput
2条回答

我怀疑你在Python2下运行程序

这会给你

TypeError: eval() arg 1 must be a string or code object

试着用Python3跑步

注意:像这样使用eval是危险的。为什么不改用int?在

如果你需要Python2版本。将input替换为raw_input,将{}替换为{}

^{pr2}$

它在我的机器上运行,确保你在评估中传递字符串。 在这里检查http://docs.python.org/library/functions.html#eval

相关问题 更多 >

    热门问题