gpt4 book ai didi

Python:按 Enter 键调用一段代码或函数

转载 作者:行者123 更新时间:2023-12-01 03:26:22 25 4
gpt4 key购买 nike

我最近开始在 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()

关于Python:按 Enter 键调用一段代码或函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41371047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com