-2

I am tring to learn python and want to know if i can do this, and how. I am trying to make binary looking code come up digit by digit, with delay. In maybe there is 15 numbers, and each repeat i would like to make it do a set of 5, with a space after.

if answer == 'MAYBE':
    deleteall()
    print("GIVE ME AN ANSWER!!!")
    time.sleep(1)
    deletelastline()
    for x in maybe:
      print(random.choice("1" "0"))
      time.sleep(0.1)
      print(random.choice("1" "0"))
      time.sleep(0.1)
      print(random.choice("1" "0"))
      time.sleep(0.1)
      print(random.choice("1" "0"))
      time.sleep(0.1)
      print(random.choice("1" "0"))
      time.sleep(0.1)
      print(" ")

However, it outputs this:

0
1
1
0
0

1
0
0
0
1

1

ext.

How do i get them on one line?!? Thx

  • 1
    Does this answer your question? [multiple prints on the same line in Python](https://stackoverflow.com/questions/5598181/multiple-prints-on-the-same-line-in-python), You know it's not forbidden to enter `[python] print same line` into the serach before posting a question about printing on the same line. – cafce25 Dec 01 '22 at 17:31
  • Unrelated; `"1" "0"` is equivalent to `"10"`, and strings are iterable, which is why your quasi-list argument works. `random.choice(["1", "0"])` would more clearly indicate what you are trying to do. – chepner Dec 01 '22 at 17:32

2 Answers2

-1

by setting end parameter you can set whatever will be after it printed. it is next line command by default "\n" so everytime it prints it gets to the next line

import time
import random
maybe = range(5)
print("GIVE ME AN ANSWER!!!")
time.sleep(1)
for x in maybe:
    print(random.choice("1" "0"),end=" ")
    time.sleep(0.1)
    print(random.choice("1" "0"),end=" ")
    time.sleep(0.1)
    print(random.choice("1" "0"),end=" ")
    time.sleep(0.1)
    print(random.choice("1" "0"),end=" ")
    time.sleep(0.1)
    print(random.choice("1" "0"),end=" ")
    time.sleep(0.1)
    print(" ")

output is:

1 1 0 1 1  
0 1 1 1 0  
0 1 0 1 0  
1 0 0 1 0  
1 0 0 0 1  
-1
import random
import time

for x in range(10):
      print(random.randint(0,1),end=' ')
      time.sleep(0.1)
print()
islam abdelmoumen
  • 662
  • 1
  • 3
  • 9