如果不运行其他子程序,如何运行子程序

时间:2015-11-29 19:54:38

标签: python-3.5

如果不运行其他子程序,如何运行子程序?我希望第一个子程序运行,如果没有,第二个子程序将运行。我试图做的任务是如果注册不在我的csv文件中,那么程序将要求用户在文本文件中输入驱动程序详细信息

import time
import csv

def Matching_Registrations():
    reading_csv=open('details_of_drivers.csv','rt')
    matching_registrations=csv.reader(reading_csv)
    for row in matching_registrations:
        for colunm in row:
            if colunm==Reg1:
                print('A fine will be sent out to this person\'s details')
                print('This is the individuals record:\n')
                print('First name: '+row[0])
                print('Surname: '+row[1])
                print('Date of birth: '+row[2])
                print('Gender: '+row[5])
                print('email: '+row[3])
                print('Adress: '+row[4])
                print('Registration plate: '+row[6]+'\n')


def Details_in_textFile():
        reading_csv=open('details_of_drivers.csv','rt')
        Details_in_textFile=csv.reader(reading_csv)
        for row in Details_in_textFile:
                for colunm in row:
                    print('please enter the details of the driver who has been speeding and is not in the database')
                    name=input('please enter the first name: ')
                    surname=input('please enter the surname: ')
                    DOB=input('please enter the Date of birth: ')
                    gender=input('please enter the gender: ')
                    email=input('please enter the email: ')
                    adress=input('please enter the adress: ')
                    registration=input('please enter the Registration plate: ')
                    file=open('details of drivers.txt','w' )
                    file.write(name+'\n'+surname+'\n'+DOB+'\n'+gender+'\n'+email+'\n'+adress+'\n'+registration)
                    file.close()
                    print('This driver was not in the database')







speed_limit_KMH=70
road_length_km=0.1
Reg1=input("Enter registration number: ")
start = time.clock()

Reg2=input("Enter second Regestration: ")

time= (time.clock() - start)

time=time/3600

driver_speed=int(road_length_km/time)

if Reg1==Reg2:

    if driver_speed>speed_limit_KMH:
        print('The driver went over the speed limit, the speed was '+(str(driver_speed))+'KPH\n')
        Matching_Registrations()
        #dont know what to do  
        Details_in_textFile()
    if driver_speed<speed_limit_KMH:
        print('The driver was driving below the speed limit, the speed was '+(str(driver_speed))+'KPH' )
    if driver_speed==speed_limit_KMH:
        print('The driver was driving on the speed limit, the speed was '+(str(driver_speed))+'KPH' )
    print('the speed limit was '+(str(speed_limit_KMH))+'KPH\n')

else:
    print('Regestrations are not a match')

1 个答案:

答案 0 :(得分:0)

你需要一些方法来检测不起作用的东西,然后从你的第一个函数(至少)传递某种“成功/失败”响应,所以你的“主”例程可以决定它是否需要运行第二个功能

您可以将布尔值设置为False,如果找到匹配项则将其更改为True并返回此

def Matching_Registrations():
    matched = False
    ...
            if colunm==Reg1:
                matched=True
                ...
    ...
    return matched

然后你可以调用像

这样的函数
if not Matching_Registrations():
    Details_in_textFile()

你可以从第一个函数中调用第二个函数而不是返回一个布尔值,但这很难读。