Python输入格式

时间:2014-12-13 16:12:17

标签: python list format

我试图让我的输入看起来像这样,但我不知道应该如何格式化它:

Enter the rainfall for January:  1
Enter the rainfall for February: 2
Enter the rainfall for March:    3

这是我得到的输入:

Enter the rainfall for January: 1
Enter the rainfall for February: 2
Enter the rainfall for March: 3

这是我的代码:

def main():
months=("January","February","March","April","May","June","July","August","September","October","November","December")
values=[0]*12

for n in range(len(values)):
    print("Enter the rainfall for", (months[n]), end=": ")
    values[n]=float(input())

main()

2 个答案:

答案 0 :(得分:1)

将print语句设为

print("Enter the rainfall for", '{0:11s}'.format(months[n]+':'), end="")

输出

Enter the rainfall for January:   1
Enter the rainfall for February:  2
Enter the rainfall for March:     3
Enter the rainfall for April:     4
Enter the rainfall for May:       5
Enter the rainfall for June:      6
Enter the rainfall for July:      7
Enter the rainfall for August:    8
Enter the rainfall for September: 9
Enter the rainfall for October:   0
Enter the rainfall for November:  1
Enter the rainfall for December:  2

答案 1 :(得分:0)

您还可以使用现有的calendar module

保存一些输入内容
from calendar import month_name
def main():
    values = []
    for month in month_name[1:]:
        values.append(float(input('Enter the rainfall for {0:11s}'.format(month + ':'))))