I’m trying to learn Python. One of the tasks is to make a box with a dynamic box.
Here’s an example of how it’s supposed to work:
JavaScript
x
6
1
text: ertioe
2
integer: 40
3
========================================
4
= ertioe =
5
========================================
6
Here’s my code:
JavaScript
1
12
12
1
print("write in something: ")
2
string = str(input())
3
4
print("write in a integer: ")
5
integer = float(input())
6
7
symbol = '='
8
9
print(symbol* (int(integer)+4))
10
print(symbol, string, symbol)
11
print(symbol* (int(integer)+4))
12
My box is all wrong:
Advertisement
Answer
You just need to centre align your string to the correct width, replace the middle print statement with:
print(symbol, string.center(int(integer)), symbol)
Edit:
Also, in case you aren’t aware, you can put the print statements within the input()
:
JavaScript
1
4
1
string = str(input("write in something: "))
2
3
integer = float(input("write in a integer: "))
4