Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A workaround to converting a number to the same amount of hashtags?

Very new to programming so please forgive my ignorance:

number = int(input("Input the number of # you want."))
hashtag = 1
total = (number * hashtag)
while hashtag != total: 
print("#")
hashtag = hashtag + 1

If i enter 4 for example, 3 hashtags will be printed, and i can’t find an alternative so that i can display 4 hashtags instead.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Python is a really cool language. You can write this in lots of ways. Here is a one liner:

number = int(input("Input the number of # you want."))
print("#" * number)

Another way with a for loop:

number = int(input("Input the number of # you want."))
for i in range(number):
    print("#", end="")  # end="" means no newline, print all in the same line

Another way with a while loop:

number = int(input("Input the number of # you want."))
counter = 0
while(counter != number):
    print("#", end="")
    counter += 1

If you want a newline at the end of each #, add a ‘\n’, like this

1st: print("#\n" * number)

2nd and 3rd: print("#") (just remove the end="")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading