PriamoRamirez
PriamoRamirez

Reputation: 71

Python: Pressing enter calls a piece of code or a function

I recently started coding on Python, one of my first challenges was to create a small script that it will be a Dice, and each time you hit ENTER it should "roll" the dice, and give you a new number.

Creating the dice itself was simple for me, since I used the randint(0,6) which gave me the number. I'm having trouble is on the pressing ENTER part. Any advise? Here's my code

 from random import randint

print "Simple Dice"

print"Press Enter to Roll Again"
Dado_Actual = randint(1,6)

print"""

 ---------
|         |
|    %d    |
|         |
 ---------
""" %Dado_Actual

Upvotes: 1

Views: 379

Answers (2)

wim
wim

Reputation: 362507

Usually you just do this with a while loop:

from random import randint

print "Simple Dice"

print"Press Enter to Roll Again"

s = ''
while s != 'q':
    print"""

     ---------
    |         |
    |    %d    |
    |         |
     ---------
    """ % randint(1,6)
    s = raw_input()

Upvotes: 2

9000
9000

Reputation: 40884

Here's a very simple idea for you:

import sys

while True:
  print "yes"
  sys.stdin.read(1) # read one byte from terminal

Modify to your taste.

Note: on Windows you'll likely need .read(2), because Enter sends \n\r, not just an \n.

Upvotes: 0

Related Questions