PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

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

 September 18, 2022     printing, python, random, while-loop     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing