Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在html页面中包含pythton骰子脚本_Python 3.x - Fatal编程技术网

Python 3.x 在html页面中包含pythton骰子脚本

Python 3.x 在html页面中包含pythton骰子脚本,python-3.x,Python 3.x,我用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)

我用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()
随机导入
#掷骰子,如果有6个,则重新掷骰子并移除6个。
def dice_roll():
值=((random.randint(1,6)))
如果值==6:
打印(值)
返回骰子卷()+骰子卷()
其他:
打印(值)
返回值
def掷骰子():
#多少骰子。
骰子数量=整数(输入(“您想掷多少个骰子?”+“\n”))
打印(“”)
打印(“\n”+”要滚动的骰子:“+str(骰子的数量)+”\n”)
最终总和=0
i=1

而我用
烧瓶
做了一个小例子。只需将这两个文件复制到某个地方,即app.py下面的
templates/
文件夹中的
.html
,然后运行
python app.py
(假设您安装了flask,否则先运行
pip install flask

app.py 导入操作系统 从烧瓶进口烧瓶 从flask导入渲染模板 随机输入 从flask导入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()
app=Flask(\uuuuu name\uuuuuu)
#掷骰子,如果有6个,则重新掷骰子并移除6个。
def dice_roll():
值=((random.randint(1,6)))
如果值==6:
打印(值)
返回骰子卷()+骰子卷()
其他:
打印(值)
返回值
def掷骰子(骰子数量):
#多少骰子。
最终总和=0
i=1

我想你可能正在搜索Django、Flask等框架。这是你想要的吗?看起来我应该去看看!非常感谢,看起来a在学习python时选择了一个糟糕的程序来编写,可能应该选择另一种语言。当然,将其翻译为Javascript并将其包含在单个HTML页面中似乎更容易选择。取决于您对正在使用的游戏辅助工具的未来计划;)看起来非常有趣,现在正在工作,所以很遗憾我不能通过python下载,但我一回到家就会尝试,这也是我学习的一个很好的例子!谢谢,最后这个例子有用吗?对于简单的脚本,只需在.html页面中扩展JS肯定会更容易。使用flask,您可以开发后端、数据库绑定、用户会话。。。不知道你打算让你的项目走哪条路:)如果它解决了你的问题,请将我的答案标记为正确的^^问候,PaulI是一个非常初学者,所以感觉我创建的东西只是做一个JS,包括一个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>