【问题标题】:Turning python function with user input into web app?将带有用户输入的python函数转换为Web应用程序?
【发布时间】:2014-02-04 22:15:10
【问题描述】:

好的,所以我已经编写了一些代码,我正在尝试了解所有内容是如何组合在一起的。我在 python 和学习 java 方面效率很高。并参加了 Web 开发课程,因此了解 HTML、小部件、一点 JS。我的问题......是否可以在 python 中创建一个hang man 游戏,然后将其发布在网页上?或类似的事情永远不会做?那么 Web 小部件是用什么制作的?

喜欢这个: 随机导入#随机导入

#Author
#File name: Lab01.py
#Date: 1/30/2014

#Purpose: allow user to play the game of hangman
#inputs:none
#outputs: Text that resembles the game of hangman
def main():
    wordList=["max","cat","basket","ship","earth","coat","ocean","beach"]#creates set of words for game to chose from

    userAnswer=raw_input("Type 1 or any button to play, 2 to quit: ")
    while userAnswer != "2":
        theWord=wordList[random.randint(0,len(wordList)-1)]
        theWordList=[]
        brickDisplay=[]
        letterList=[]
        count=0#sets count to 0
        statementCount=0#sets statement count to 0

        for i in range (len(theWord)):
            theWordList.append(theWord[i])#creates list of each character of the word   
        brickDisplay=[]
        for i in range (len(theWordList)):
            brickDisplay.append("-")#Creates the same number of underscores that there are letters in the word
        print brickDisplayMaker(brickDisplay)
        letterChoice=raw_input("Enter a letter: ")
        conditionEnd=conditionChecker(statementCount)#updates conditionEnd
        wordEnd=wordChecker(theWordList,brickDisplay)#updates wordEnd
        while wordEnd != "stop" and conditionEnd != "stop":
                while letterChoice in letterList: #Checks if letter has already been entered by user
                    print "You've already entered that"
                    letterChoice=raw_input("Enter a letter: ")

                else:
                    letterList.append(letterChoice)
                    for i in range(len(theWordList)):
                        if theWordList[i]==letterChoice:
                            count=count+1#counts the number of times letterChoice was in the word
                            brickDisplay[i]=theWordList[i]
                    print brickDisplayMaker(brickDisplay)
                    if count==0:
                        statementCount=statementCount+1
                        statementProducer(statementCount,theWord)
                    conditionEnd=conditionChecker(statementCount)
                    wordEnd=wordChecker(theWordList,brickDisplay)
                    count=0
                    if wordEnd=="stop":
                        print "You've Won!"
                    elif conditionEnd=="stop":
                        print "You have lost, the word was "+theWord #lets user know what the word was
                    else:    
                        letterChoice=raw_input("Enter a letter: ")


        userAnswer=raw_input("Type 1 or any button to play, 2 to quit: ")
    print "Thanks for playing" #exists main
#Purpose: Check if words are the same
#Inputs: theWordList, brickDisplay
#outputs: "stop" or "go"
def wordChecker(theWordList,brickDisplay):
    count=0
    for i in range(len(theWordList)):
        if theWordList[i]!=brickDisplay[i]:
            count=count+1
    if count==0:
        return "stop"
    else:
        return "go"
#Purpose: Check if statement count equals 6
#inputs: StatementCount
#outputs: "stop" or "go"
def conditionChecker(statementCount):
    if statementCount==6:
        return "stop"
    else:
        return "go"

#Purpose: Create the brickDisplay with spaces between the underscores
#Inputs: brickDisplay
#Outputs:bricksDisplay with spaces between the underscores
def brickDisplayMaker(brickDisplay):
    officialBrickDisplay=""
    for ndx in brickDisplay:
        officialBrickDisplay=officialBrickDisplay+ndx+" "#creates new list with spaces between underscores
    return officialBrickDisplay 
#Purpose: Produces statement based on the statementCount
#Inputs: statementCount
#Outputs: Statement
def statementProducer(statementCount,theWord):
    if statementCount ==1:
        print "Draw body part: head"
    elif statementCount==2:
        print "Draw body part: body"
    elif statementCount==3:
        print "Draw body part: left arm"
    elif statementCount==4:
        print "Draw body part: right arm"
    elif statementCount==5:
        print "Draw body part: left leg"
    else:
        print "Draw body part: right leg"



main()

【问题讨论】:

  • 你可以用python做网站,是的。
  • 是的..你可以试试skulpt,它是用javascript实现的python。因此,任何纯 Python 代码都应该在浏览器中运行而无需更改任何内容。
  • 所以我正在查看那个网站,我可以在一个网站中输入我的 python。但是说我正在创建一个网站,我只想让我的 python 的输出显示在网站上,而不是代码(我的网站代码中)

标签: javascript python html web widget


【解决方案1】:

这取决于您要使用的技术。如果你想坚持使用 Python,你可以使用 Django,例如:https://www.djangoproject.com/

对于 JavaScript,有几十个框架; This is a famous website 使用不同的框架来执行相同的任务,一个简单的待办事项应用程序。

最终,这取决于您的喜好。就个人而言,我非常喜欢Meteor,但这对你来说可能有点过头了,因为它是为实时 Web 应用程序设计的。这完全取决于您的目标。

【讨论】:

  • 谢谢你的回答。所以我下载django?然后我会给它一个py文件或输入我的代码,它会给我一些我可以放入html的东西。还是我不明白它是如何工作的?
  • 其实,并不容易。你介意解释一下你想做什么吗?不过,我不知道 skulpt。所以我不确定这是否能满足你的需要。
  • 只是一个随机的例子,假设我想在 html 上发布这个:
  • 我会在问题中发布它
猜你喜欢
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多