我需要编写一个程序,要求用户输入一个数字n,其中-6 <n <93。
输出:输入起始编号:12
12 13 14 15 16 17 18
数字需要使用字段宽度2打印并且右对齐。字段需要由单个空格分隔。最后一个字段后应该没有空格。
到目前为止这是我的代码:
a = eval(input('Enter the start number : ',end='\n'))
for n in range(a,a+7):
print("{0:>2}").format(n)
print()
但它说:
File "C:/Users/Nathan/Documents/row.py", line 5, in <module>
a = eval(input('Enter the start number : ',end='\n'))
builtins.TypeError: input() takes no keyword arguments
请帮忙
首先,input
函数返回string
。你应该把它作为整数。
你也有一些语法错误,仅举几例:
1)你打印后放.format
,但它应该在print
内和字符串之后。
2)input
函数不接受end
论证。并且python为你提供了这个错误:TypeError: input() takes no keyword arguments
3)格式化模式不正确。
这段代码可以满足您的需求:
a = int(input('Enter the start number : '))
for n in range(a, a+7):
print("{:02d}".format(n), end=' ')
OUTPUT:
Enter the start number : 12
12 13 14 15 16 17 18
你不能传递\ n到输入beacouse是一个特殊字符。
如果您想要白线,请在输入后添加另一个print()。
input()
没有采取最终的论点,只有print()
。