0

I am trying to get my input look like this, but i dont know how should i format it:

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

This is the inputt i get:

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

Here is my code:

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 Answers 2

1

Make the print statement as

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

Output

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
Sign up to request clarification or add additional context in comments.

1 Comment

Just to be clear here, it works since we know the lengths of all the months. In a general case, we'll find the max length of the strings and then perform the padding using something like s.ljust(max_len).
0

You can also save some typing by using the existing 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 + ':'))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.