简体   繁体   English

从 HTML 中的循环打印结果

[英]Printing the results from loop in HTML

I have a simple Python script that accepts user input as letters.我有一个简单的 Python 脚本,它接受用户输入作为字母。 Then it looks up a json dictionary for valid words that can be generated with provided letters and print valid words to screen.然后它在 json 字典中查找可以使用提供的字母生成的有效单词,并将有效单词打印到屏幕上。

I created simple website using pythonanywhere & flask.我使用 pythonanywhere 和 flask 创建了简单的网站。 There is a textbox for input and user enter letters.有一个用于输入和用户输入字母的文本框。 I managed to make it worked until output part.我设法让它工作到 output 部分。 What I try to do is to print output (valid words) to html page.我尝试做的是将 output (有效字)打印到 html 页面。 I can only print the first string value in the loop.我只能打印循环中的第一个字符串值。 Please see screenshot请看截图

Any help would be appreciated.任何帮助,将不胜感激。

Actually, script works as intended in local.实际上,脚本在本地按预期工作。 I can get valid words using "print(kelime)" in the last part.我可以在最后一部分使用“print(kelime)”得到有效的单词。请看截图

My index.html for input and output我的 index.html 用于输入和 output

 <form action="/index" method = "POST"> <p>Harfler <input type = "text" name = "harfler" /></p> <p><input type = "submit" value = "Submit" /></p> </form> <h3>{{harfler}}</h3> <h3>{{harfsayisi}}</h3> <h3>{{kelime}}</h3> </form>

My script.py我的脚本.py

from flask import Flask, request,render_template,json

app = Flask(__name__)

@app.route('/index', methods = ['POST', 'GET'])
def data():
    if request.method == 'GET':
        return render_template('index.html')

    if request.method == 'POST':
        harfler = request.form['harfler']

        #json dosyasını açıyoruz
        # = open the JSON file
        with open('mysite/gts5.json',encoding="utf8") as f:
            data = json.load(f)

        # harfler listesindeki her bir harfin sayısını hesaplıyorum
        # = calculating the number of each letter in the letters list
        harfler_harf_sayisi = {}
        for i in harfler:
            harfler_harf_sayisi[i] = harfler.count(i)
        harfsayisi=harfler_harf_sayisi

        #json dosyasındaki her bir kelimeyi kelimeler listesine ekliyorum. json dosyasindaki (yani sozlukteki) madde key'ine karsilik gelen her value bizim icin kelimedir.
        # = I'm adding each word in the json file to the words list. Each value corresponding to the item key in the json file (ie in the dictionary) is a word for us.
        kelimeler = []
        for i in range(len(data)):
            kelimeler.append(data[i]['madde'])

        #kelimeler listesindeki duplicate kayitlari ucuruyoruz
        # = we are removing duplicate records in the word list
        kelimeler = list(dict.fromkeys(kelimeler))

        #harflerdeki her harf ayni zamanda kelimede mevcut mu kontrolü burada yapiyoruz
        # = Here we check whether each letter in the letters is also present in the word.
        for kelime in kelimeler:
            hepsi_var = all([harf in harfler for harf in kelime])

            #her bir kelimedeki harf sayisini hesapliyorum
            # = calculate the number of letters in each word
            kelime_harf_sayisi = {}
            for i in kelime:
                kelime_harf_sayisi[i] = kelime.count(i)

            #harfler listesindeki her harf kelimede de varsa...
            # = If every letter in the list of letters is also in the word...
            if hepsi_var == True:

                #bu nokta çok kritik. burada kelimenin gecerli olabilmesi icin harflerdeki harf sayisinin kelimedeki harf sayisindan olması gerektigini soyluyoruz. ayrica kelime tek harften oluşuyorsa onu göz ardı ediyoruz.
                # = This point is very critical. Here we say that for the word to be valid, the number of letters in the letters must be the number of letters in the word. Also, if the word consists of only one letter, we ignore it.
                gecerli_kelime = all(harfler_harf_sayisi[k] >= kelime_harf_sayisi[k] for k in kelime_harf_sayisi) and (len(kelime_harf_sayisi) > 1)

                if gecerli_kelime == True:

                    print(kelime)
                    return render_template('index.html',harfler=harfler,harfsayisi=harfsayisi,kelime=kelime)
            else:
                continue

OK, I figured it out myself.好的,我自己想通了。 I'm posting here in case it helps someone.我在这里发帖以防它对某人有帮助。

I had to put all outputs in the loop into a new variable.我必须将循环中的所有输出放入一个新变量中。

New code would be:新代码将是:

def harfgiris():

harfler = input("Harfleri girin?" + ' ')
print(list (harfler))
return harfler

def sonuc():
import json

#json dosyasını açıyoruz
with open('C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\gts5.json',encoding="utf8") as f:
    data = json.load(f)

# harfler listesindeki her bir harfin sayısını hesaplıyorum
harfler=harfgiris()
harfler_harf_sayisi = {}
for i in harfler:
    harfler_harf_sayisi[i] = harfler.count(i)

#json dosyasındaki her bir kelimeyi kelimeler listesine ekliyorum. json dosyasindaki (yani sozlukteki) 'madde' key'ine karsilik gelen her value bizim icin kelimedir.
kelimeler = []
for i in range(len(data)):
    kelimeler.append(data[i]['madde'])

#kelimeler listesindeki duplicate kayitlari ucuruyoruz
kelimeler = list(dict.fromkeys(kelimeler))

kelimelistesi=""
#harflerdeki her harf ayni zamanda kelimede mevcut mu kontrolü burada yapiyoruz
for kelime in kelimeler:
    
    hepsi_var = all([harf in harfler for harf in kelime])
    
    #her bir kelimedeki harf sayisini hesapliyorum
    kelime_harf_sayisi = {}
    for i in kelime:
            kelime_harf_sayisi[i] = kelime.count(i)
            
    #harfler listesindeki her harf kelimede de varsa...
    if hepsi_var == True:  

            #bu nokta çok kritik. burada kelimenin gecerli olabilmesi icin harflerdeki harf sayisinin kelimedeki harf sayisindan olması gerektigini soyluyoruz. ayrica kelime tek harften oluşuyorsa onu göz ardı ediyoruz.
            gecerli_kelime_sarti = all(harfler_harf_sayisi[k] >= kelime_harf_sayisi[k] for k in kelime_harf_sayisi) and (len(kelime_harf_sayisi) > 1)
            
            
            if gecerli_kelime_sarti == True:
                    
                    #konsola yazdırırken bunu da kullanabilirdik
                    #print(kelime)
                    
                    #olası tüm kelimeleri bir değişkene atıyoruz
                    kelimelistesi=kelimelistesi + "\n" + kelime
                   
    else:
        continue

#olası tüm kelimeleri yazdırıyoruz
print(kelimelistesi)

sonuc()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM