子程序不工作,错误消息说变量未定义 [英] Subroutine not working, error message saying variable is not defined

查看:40
本文介绍了子程序不工作,错误消息说变量未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个子程序,我认为这是我的问题的原因,这是我的代码:

def sub1():dob=input('请输入此人的日期(dd) :')而 dob 不在 ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']:print('你输入的日期不正确')dob=input('请输入此人的日期(dd) :')子1()如果月份 == '02' :如果 dob == ['30','31'] :print('你输入错误')子1()

变量月只是 01,02,03,04,05,06,07,08,09,10,11,12

错误信息是:文件C:/Users/Akshay Patel/Documents/TASK 2/task 两个月 dob.py",第 13 行,在如果 dob == ['30','31'] :

NameError: name 'dob' 未定义

解决方案

变量 dobsub1 的局部变量,因此您无法在全局范围内看到它.

您可能想退货:

def sub1():dob=input('请输入此人的日期(dd) :')而 dob 不在 ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']:print('您输入的日期不正确')dob=input('请输入此人的日期(dd) :')返回 dob ###########dob = sub1() ########如果月份 == '02' :if dob in ['30','31'] : ######### in not ==print('你输入错误')dob = sub1() ##############

<小时>

我个人会稍微重构一下您的代码:

def inputDay (month):#february 不尊重闰年maxDay = [42, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] [月]时间 = 0而 dob >maxDay 或 dob <1:dob = int(input('请输入日期:'))返回 dob月 = 2 #相应地改变这个值日 = 输入日(月)print('{}/{}'.format(month, day))

<小时>

如果你想输入一个完整的日期,你可以考虑使用这个:

from datetime import datetime定义输入日期():为真:try: return datetime.strptime(input('Enter date (m/d/yyyy): '), '%m/%d/%Y')除了值错误:通过a = inputDate()打印(一天,一个月,一年)

i am using a subroutine and i think it is the cause of my problem, here is my code :

def sub1():
    dob=input('Please enter the date of the person (dd) : ')
    while dob not in ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']:
        print('you have entered an incorrect day')
        dob=input('Please enter the date of the person (dd) : ')
sub1()
if month == '02' :
    if dob == ['30','31'] :
        print('You have entered incorrecty')
        sub1()

The variable month is just 01,02,03,04,05,06,07,08,09,10,11,12

The error message is : File "C:/Users/Akshay Patel/Documents/TASK 2/task two month dob.py", line 13, in if dob == ['30','31'] :

NameError: name 'dob' is not defined

解决方案

The variable dob is local to sub1 and hence you cannot see it at global scope.

You might want to return it:

def sub1():
    dob=input('Please enter the date of the person (dd) : ')
    while dob not in ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']:
        print('you have entered an incorrect day')
        dob=input('Please enter the date of the person (dd) : ')
    return dob ###########
dob = sub1() ########
if month == '02' :
    if dob in ['30','31'] : ######### in not ==
        print('You have entered incorrecty')
        dob = sub1() ##############


I personally would refactor your code a bit:

def inputDay (month):
    #february doesn't respect leap years
    maxDay = [42, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] [month]
    dob = 0
    while dob > maxDay or dob < 1:
        dob = int(input('Please enter day of month: '))
    return dob

month = 2 #change this value accordingly
day = inputDay(month)
print('{}/{}'.format(month, day))


If at some moment you want to input a whole date, you can consider using this:

from datetime import datetime

def inputDate():
    while True:
        try: return datetime.strptime(input('Enter date (m/d/yyyy): '), '%m/%d/%Y')
        except ValueError: pass

a = inputDate()
print(a.day, a.month, a.year)

这篇关于子程序不工作,错误消息说变量未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆