我在python中为我正在玩的角色扮演游戏创建了骰子滚动脚本。
import random
# Roll dice, if 6 reroll and remove 6.
def dice_roll():
value = ((random.randint(1, 6)))
if value == 6:
print(value)
return dice_roll() + dice_roll()
else:
print(value)
return value
def roll_dices():
# How many dice.
number_of_dices = int(input("How many dice would you like to roll?" + "\n"))
print(" ")
print("\n" + "Dices to roll: " + str(number_of_dices) + "\n")
final_sum = 0
i = 1
while i <= number_of_dices:
final_sum += dice_roll()
i += 1
print("The final sum is: " + str(final_sum))
roll_dices()
我一直在尝试寻找一种在HTML页面或exe上使用它的方法,但没有成功。关于如何将代码转换为可以在终端或cmd之外运行的任何提示?
答案 0 :(得分:0)
使用flask
做一个小例子。只需将这两个文件复制到app.py下.html
文件夹中的templates/
中,然后运行python app.py
(假设您已安装flask,否则请预先运行pip install flask
)>
app.py 导入操作系统 从烧瓶进口烧瓶 从烧瓶导入render_template 随机导入 从烧瓶导入jsonify
app = Flask(__name__)
# Roll dice, if 6 reroll and remove 6.
def dice_roll():
value = ((random.randint(1, 6)))
if value == 6:
print(value)
return dice_roll() + dice_roll()
else:
print(value)
return value
def roll_dices(number_of_dices):
# How many dice.
final_sum = 0
i = 1
while i <= number_of_dices:
final_sum += dice_roll()
i += 1
#print("The final sum is: " + str(final_sum))
return final_sum
@app.route('/')
def index():
return render_template('index.html')
@app.route('/rollNdice/<int:num>')
def rollDice(num):
return jsonify({"num":num,"sum":roll_dices(num)})
app.run()
template / index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="/action_page.php">
How many dice would you like to roll? <input type="range" id="numDice" value="3" max=6 min=1><br>
The final sum is: <p id="sum"></p>
<button type="button" onclick="getRoll()">Click Me!</button>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
function getRoll() {
var x = $("#numDice").val();
$.getJSON( "/rollNdice/"+x, function( data ) {
document.getElementById("sum").innerHTML = data["sum"] + " rolling " + data["num"] +" dice</br>"
});
}
</script>
</html>
看起来很斯巴达,但我希望它能帮助:)