【发布时间】:2020-07-18 15:44:42
【问题描述】:
我想使用来自 html 的输入作为我的 Flask 中的一个函数:
from flask import Flask, url_for, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
app = Flask(__name__)
@app.route("/")
def homepage() :
return render_template("index.html", make_cards = make_cards(user_deck), make_cards_u = user_cards(), result = find_winner(user_deck, computer_deck))
class user_cards (FlaskForm):
SUITS = StringField('suits', validators=[DataRequired()])
SEQUENCE = StringField('stringfield', validators=[DataRequired()])
submit = SubmitField('Check!')
if __name__ == '__main__':
# print(poker_hand_ranking(["10H", "JH", "QH", "AH", "KH"]))
user_deck = user_cards()
computer_deck = make_cards(user_deck)
find_winner(user_deck, computer_deck)
app.run(debug=True)
我希望这个程序与用户交互,所以我没有定义make_cards_u,并且想要卡片的用户输入。所以我的HTML如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF=8">
<title> Poker Game </title>
<link rel="stylesheet" href ="{{url_for('static', filename='css/style.css')}}">
</head>
<body>
<h1> Welcome to Gaming Poker</h1>
<h3>Computer cards: {{make_cards}} <br>
Your cards: <input> {{make_cards_u}} </input> </h3>
<h2>Results: {{result}}</h2>
</body>
</html>
但是,我收到一个错误
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that
needed to interface with the current application object in some way.
To solve this, set up an application context with app.app_context().
See the documentation for more information.
我的其他python代码如下:
SEQUENCE = ['2', '3', '4', '5', '6', '7', '8', '9', '10',
'J', 'Q', 'K', 'A']
VALUES = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14}
SUITS = ['S', 'H', 'C', 'D']
# The computer gets 5 cards
def make_cards(deck = None):
result_deck = set()
while (len(result_deck) < 5):
card = random.choice(SEQUENCE) + random.choice(SUITS)
if (deck != None):
if (card not in deck):
result_deck.add(card)
else:
result_deck.add(card)
return list(result_deck)
def find_winner(user_deck, computer_deck):
user_rank = poker_hand_ranking(user_deck)
computer_rank = poker_hand_ranking(computer_deck)
if (user_rank[0] < computer_rank[0]):
print('Winner is user')
elif (user_rank[0] > computer_rank[0]):
print('Winner is computer')
else:
print('Draw!')
print('User:', user_deck, user_rank[1])
print('Computer:', computer_deck, computer_rank[1])
另外,我有在 if elif 语句中定义变量的代码,这太长了,无法提出这个问题。 poker_hand_ranking 定义如下:
def poker_hand_ranking(deck):
hand = deck
suits = sorted([card[-1] for card in hand])
faces = sorted([card[:-1] for card in hand])
if is_royal_flush(hand, suits, faces):
return 1, "You have a Royal Flush"
elif is_straight_flush(hand, suits, faces):
return 2, "You have a Straight Flush"
elif is_four_of_a_kind(hand, suits, faces):
return 3, "You have four of a Kind"
elif is_full_house(hand, suits, faces):
return 4, "You have a full House"
elif is_flush(hand, suits, faces):
return 5, "You have a flush"
elif is_straight(hand, suits, faces):
return 6, "You have a straight"
elif is_three_of_a_kind(hand, suits, faces):
return 7, "You have a three of a Kind"
elif is_two_pairs(hand, suits, faces):
return 8, "You have a two Pair"
elif is_one_pair(hand, suits, faces):
return 9, "You have a pair"
else:
#if none of these counts:
return 10, "High Card"
如何让用户输入在 Flask 中进行交互?
【问题讨论】:
-
在问题中包含
make_cards和find_winner方法。 -
@arsho 谢谢你,我已经这样做了!
标签: python html flask flask-wtforms