Sunday, September 18, 2022

[FIXED] How do I make my Print Statement print on only one line?

Issue

In this program I am trying to print my code as "random letters here" instead of "r" "a" "n" "d" "o" "m" etc.

import random
import time

All = "abcdefghijklmnopqrstuvwxyz1234567890"
i = 0

while i < 6:
    time.sleep(1)
    print(All[random.randint(1,35)])
    i += 1

Solution

You can pass end='' to the print statement so that subsequent prints are not separated by a newline, which is the default behavior. Also, here it makes sense to use a range iterator with for instead of while.

Note: I've taken the liberty to cut the sleep time in half, so it's easier to test it out.

import random
import time

All = "abcdefghijklmnopqrstuvwxyz1234567890"

for _ in range(6):
    time.sleep(0.5)
    print(All[random.randint(1, 35)], end='')


Answered By - rv.kvetch
Answer Checked By - Candace Johnson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.