为什么变量显示为未定义?

时间:2018-07-03 16:26:55

标签: python-3.x flask

我正在使用flask在http://localhost:5000/上创建一个网络表单,我的目标是该表单提示用户输入两个输入,然后将这些输入写入名为test2.py和test3.py的两个新python3文件中。变量“ text”和“ text1”保持未定义状态。这是代码及其错误:

代码:

#include <iostream>
#include <vector>
#include <climits>
#include <cfloat>
using namespace std;
double mean{};
    double min=FLT_MAX;
    double max=FLT_MIN;
vector<double>numbers{};
char select;
char list();
void print_nums(vector<double>&numbers);
void program_func();
double nums_mean(double);
char switch_function(char);
void display_nums_mean(double);
int main(){


   program_func();


        return 0;
}
void program_func(){
    while(select!='q'&&select!='Q'){
       list();
  switch_function(select);

        }
    }
char list(){
     cout<<"Please select an Order\n";
        cout<<"***********************\n";
        cout<<"P-Print numbers\n";
        cout<<"A-Add a number\n";
        cout<<"M-Display the numbers mean\n";
        cout<<"S-Display smallest number\n";
        cout<<"L-Display largest number\n";
        cout<<"Q-Quit\n";
        cout<<"Order " ;
        cin>>select;
        cout<<endl;
    return select;

    }
void print_nums(const vector <double> &numbers){
    if(numbers.size()!=0)
     for(auto value : numbers)
              cout<<value<<endl;
            else
                cout<<"Array is empty"<<endl;

    }
void add_num(){
        cout<<"How many numbers will be added to the list : ";
        unsigned int num_of_nums;
        double num;
        cin>>num_of_nums;
        cout<<endl;
        cout<<"add numbers to the list : ";
        for(unsigned int i=0;i<num_of_nums;i++){
            cin>>num;
            numbers.push_back(num);
            }
        return;
        }
double nums_mean(double mean){
    double sum{};
    for(unsigned int i=0;i<numbers.size();++i){
        sum+=numbers.at(i);

        }
    mean = sum/(numbers.size());


    return mean;


    }
double small_num(double min){
    for(unsigned int i=0;i<numbers.size();++i){

                if(min>=numbers.at(i)){

                    min = numbers.at(i);
                    }

            }
            return min;
    }
double large_num(double max){
    for(unsigned int i=0;i<numbers.size();++i){
                if(max<=numbers.at(i)){

                    max = numbers.at(i);
                }

            }
            return max;

    }
void display_nums_mean(double mean){
    cout<<"The mean of the numbers is : "<<mean<<endl;


    }
void display_nums_min(double min){
    cout<<"The minimum number is : "<<min<<endl;

    }
void display_nums_max(double max){
    cout<<"The maximum number is : "<<max<<endl;
    return;
    }
char switch_function(char select){
    switch(select){
        case 'p':
        case 'P':
                 print_nums(numbers);
                 break;
        case 'a':
        case 'A':
                 add_num();
                 break;
        case 'm':
        case 'M':
                 mean=nums_mean(mean);
                 display_nums_mean(mean);
                 break;
        case 's':
        case 'S':
                 min=small_num(min);
                display_nums_min(min);
                 break;
        case 'l':
        case 'L':
                 max=large_num(max);
                 display_nums_max(max);
                 break;
        case 'q':
        case 'Q':
         return select;
        default:
         cout<<"Please Enter a valid character "<<endl;


    }

    return select;   
    }

错误:

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('my-form.html')

@app.route('/', methods=['POST'])
def my_form_post():
    text = request.form['text']
    text1 = request.form['text1']

with open('test2.py', 'w+') as file:
    file.write(text)

with open('test3.py', 'w+') as file:
    file.write(text1)

谢谢!

1 个答案:

答案 0 :(得分:0)

这是因为texttext1是在函数中定义的,而文件处理不在外面。文件处理应在post方法内部。