[英]Python: Pressing enter calls a piece of code or a function
我最近開始用Python進行編碼,我的第一個挑戰是創建一個小小的腳本,它將成為一個骰子,每當您按ENTER鍵時,它應該“擲骰子”,並給您一個新的數字。
對我來說,創建骰子本身很簡單,因為我使用了randint(0,6)來給我數字。 我在按ENTER鍵時遇到麻煩。 有什么建議嗎? 這是我的代碼
from random import randint
print "Simple Dice"
print"Press Enter to Roll Again"
Dado_Actual = randint(1,6)
print"""
---------
| |
| %d |
| |
---------
""" %Dado_Actual
通常,您只需要使用while
循環即可:
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()
這是一個非常簡單的想法:
import sys
while True:
print "yes"
sys.stdin.read(1) # read one byte from terminal
修改您的口味。
注意:在Windows上,您可能需要.read(2)
,因為Enter發送\\n\\r
,而不僅僅是\\n
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.