如何在python中使用用户输入打印水平行数

问题描述 投票:1回答:3

我需要编写一个程序,要求用户输入一个数字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

请帮忙

python-3.x
3个回答
1
投票

首先,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

1
投票

你不能传递\ n到输入beacouse是一个特殊字符。

如果您想要白线,请在输入后添加另一个print()。


1
投票

input()没有采取最终的论点,只有print()

© www.soinside.com 2019 - 2024. All rights reserved.