会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132454个问题

from threading import Thread,Event
from time import sleep
'''
几分钟一辆车
人上车和车辆可以上人是要同时进行的,车辆不在站内和人等待车也是同时进行的
'''

def car():
    num=0
    #global count,有event就不用count了
    while True:
        if event.is_set():      #count%5!=0:
            print('车已到站,可以上车')
            sleep(1)
            #count+=1   #就是上了四个人后开走了
            num+=1
            if num%5==0:
                event.clear()       #上了五个人开走了
        else:
            print("车开走了....")
            sleep(6)
            #count=1        
            #过了六秒后又开来了
            event.set()     #重新变成真的
def person():
    while True:
        #if count%5!=0:
        if event.is_set():
            print('上车!!')
            sleep(1)
        else:
            print('等待车!!!') #用count的时候,等待一直在刷,程序没有停下来
            event.wait()        #阻塞线程
            #sleep(1)
if __name__=="__main__":
    event=Event()
    count=1
    t1=Thread(target=car)
    t2=Thread(target=person)
    t1.start()
    t2.start()

老师,这个event是在车方法里面变成True的,怎么在人方法里面也是True了,两个方法不是一个栈帧里面的,他们的调用event方法也能共用?

Python 全系列/第三阶段:Python 网络与并发编程/并发编程 35612楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask数据库 35616楼

# 编程计算水费
import plotly as py
from plotly import figure_factory as FF
import plotly.graph_objs as pygo
import pandas as pd
import numpy as np
import csv

# 写一个txt文件
f1 = open(r"water.txt","w",encoding="utf-8")
data = "账号 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月\n\
0000359333 772 789 806 847 880 901 950 991 1022 1043 1064 1089 1114\n\
0000359305 121 132 145 156 168 179 192 206 219 230 246 258 273\n\
0000359708 1008 1046 1102 1167 1209 1255 1311 1362 1407 1453 1512 1563 1604\n\
0000359504 541 567 590 622 651 689 701 732 758 775 796 814 847\n\
0000359209 401 412 441 466 479 490 508 522 541 572 603 637 666"
f1.write(data)

# 计算txt文件数据
f2=open("water.txt","r")
head=f2.readline()
sum=0
for line in f2.readlines():
    l=line.split()
    for i in range(len(l)/2):
        s=int(l[i*2+1])+int(l[i*2+2])*1.05
        sum+=s
print(sum)

# 将txt文件转为csv文件
data_txt = np.loadtxt('water.txt')
data_txtDF = pd.DataFrame(data_txt)
data_txtDF.to_csv('water.csv',index=False)

#绘制折线图
pyplt = py.offline.plot     #离线模式
data = pd.read_csv("water.csv",encoding="GBK")
table = FF.create_table(data)
data["水费"]=sum
xdata = data["账号"].tolist()   # 取账户这一列,做列表
ydata = data["水费"].tolist()     # 取水费这一列,做列表
trace = pygo.Scatter(x=xdata,y=ydata,name="水费")     # 水费折线
money = pygo.data(trace)
layout = pygo.Layout(title="用户一年的水费")       #图的标题
money= pygo.Figure(data=money,layout=layout)
print(money)

#关闭文件
f1.close()
f2.close()

创建水量文件“water.txt”文件,其内容第一列为账号,后面是每个月的用水量(后一个数-前一个数),共十二个月。每立方米需付1.05元。 编程计算每户一年的水费,并画出折线图。

0000359333 772  789 806 847 880 901 950 991 1022 1043 1064 1089 1114

0000359305 121  132 145 156 168 179 192 206 219 230 246 258 273

0000359708 1008 1046 1102 1167 1209 1255 1311 1362 1407 1453 1512 1563 1604

0000359504 541 567 590 622 651 689 701 732 758 775 796 814 847

0000359209 401 412 441 466 479 490 508 522 541 572 603 637 666。


老师,这道题我是先写一个txt文件,然后计算txt文件中的数据,后转成表格形式的csv文件,再用csv文件绘制折线图。因为这是昨天晚上写的,那个时候提问老师也下班了,而且掌握的不熟练,程序不长所以我就没有写一下运行一下。报的错误为:


txt文件可以正常输出,但txt文件计算数据时为0,接下来的步骤全是按照平常课程老师举的例子来写的。麻烦老师花点时间看看题目教教我,求教,谢谢!


Python 全系列/第二阶段:Python 深入与提高/文件处理 35617楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解 35618楼

import java.sql.Connection;
import java.sql.PreparedStatement;

public class PreparedStatementDemo {
    //向Departments表中插入数据
    public void insertDepartment(String departmentName,int locationId){
        Connection conn=null;
        PreparedStatement ps=null;
        try{
            conn=jdbcUtill.getConnection();
            ps=conn.prepareStatement("insert into departments values (default ,?,?)");
            ps.setString(1,departmentName);
            ps.setInt(2,locationId);
            boolean bo=ps.execute();
            System.out.println(bo);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbcUtill.closeAll(ps,conn,null);
        }
    }

    public static void main(String[] args) {
        PreparedStatementDemo pr=new PreparedStatementDemo();
        pr.insertDepartment("下调部",4);
    }

}
import java.sql.*;
import java.util.ResourceBundle;

public class jdbcUtill {
    /**
     * 工具类
     */

    private static String driver;
    private static String jdbcUrl;
    private static String username;
    private static String userpassword;
    static{
        //读取Properties文件
        ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        jdbcUrl=bundle.getString("jdbcUrl");
        username=bundle.getString("username");
        userpassword=bundle.getString("userpassword");
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取Connection对象
    public static Connection getConnection(){
        Connection conn=null;
        try{
            conn= DriverManager.getConnection(jdbcUrl,username,userpassword);
        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
    }
    //关闭Connection
    public static void closeConnection(Connection coon){
        if(coon!=null){
            try {
                coon.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //关闭Statement
    public static  void closeStatement(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //关闭ResultSet连接
    public static void clossResultset(ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //关闭资源
    public static void closeAll(Statement state,Connection coon,ResultSet rs){
        closeConnection(coon);
        closeStatement(state);
        clossResultset(rs);
    }

}

image.png老师,请问一下我这个为什么报错啊

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 35622楼
JAVA 全系列/第十阶段:百战旅游网项目/百战旅游网 35623楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 35624楼
Python 全系列/第一阶段:Python入门/面向对象 35625楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637