+ 1
Can someone help me shorten this?
This is my first real program outside of the stuff I made for my lessons. Is there a way to shorten it? Thanks https://code.sololearn.com/cK7JdO092WDb/?ref=app
10 Respuestas
+ 3
Alex
Enter any length of string, doesn't matter
a = input()
x = len(a)
for i in range(1, x + 1):
print("*", a[0 : i], "* ")
print("\nThat was rather underwhelming, wasn't it? Oh well, you can still leave a like.")
+ 2
This is a bit shorter (I see it's pretty much what Calvin Thomas said)
for i in range(len(a := input())):
print("*", a[:i], "*")
print("\nThat was rather underwhelming, wasn't it? Oh well, you can still leave a like.")
+ 2
Thanks Calvin Thomas good points.
+ 1
Thank you very much
+ 1
Here's a one-liner that might help:
for i in range(len(k := input())): print("* ", k[:i+1], " *")
# Hope this helps
+ 1
David Ashton Your code actually doesn't print the last letter. Also, it prints an unnecessary space character in the beginning. This was why I had added 'i+1' in my code.
+ 1
David Ashton You're welcome.
+ 1
Calvin Thomas it makes total sense, welcome
0
Try to create a comprehension list first and iterate through the list with for loop as below:
a = input()
u = ['* '+ a[0:i] + ' *' for i in range(1,len(a)+1)]
for j in u:
print(j)
0
Innocent Salimoni Why not just a[:i]?