为什么此代码不起作用以及如何修复它?
Why this code doesn't work and how to repair it?
我写了一个代码,这是一个简单的程序,有 2 个输入和打印。我添加了一些代码来防止名称和年份是正确的。当来自输入的数据正确时,我的程序可以正常工作,但当它不存在时,有相同的消息并且输入可以再次输入,但是在分配给变量新词之后,打印旧词:/我使用 Python 3.7.3
import re
def name_check(x):
pattern1=r'[A-Za-z]'
if re.match(pattern1,x):
pass
else:
print ('This is not your name.')
give_name()
def year_check(y):
pattern2=r'(\d)'
if re.match(pattern2,y):
pass
else:
print ('This is not your year of birth.')
give_year()
def printing(x,y):
try:
print('Hey,',x,',in',int(y)+100,'year you will have 100 years.')
except:
print ('An error occured.')
def give_name():
x=str(input('Enter your name: '))
name_check(x)
return x
def give_year():
y=input('Enter your year of birth: ')
year_check(y)
return y
def program():
x=give_name()
y=give_year()
printing(x,y)
program()
问题是您只在第一次捕获变量(x 和 y)。
试试这个:
import re
def name_check(x):
pattern1=r'[A-Za-z]'
if re.match(pattern1,x):
return True
else:
print ('This is not your name.')
return False
def year_check(y):
pattern2=r'(\d)'
if re.match(pattern2,y):
return True
else:
print ('This is not your year of birth.')
return False
def printing(x,y):
print(x,y)
try:
print('Hey,',x,',in',int(y)+100,'year you will have 100 years.')
except:
print ('An error occured.')
def give_name():
x=str(input('Enter your name: '))
while not name_check(x):
x=str(input('Enter your name: '))
return x
def give_year():
y=input('Enter your year of birth: ')
while not year_check(y):
y=input('Enter your year of birth: ')
return y
def program():
x=give_name()
y=give_year()
printing(x,y)
program()
在您的程序中,x
和 y
在链函数调用后不会改变。你应该在你的 year_check
和 name_check
函数中使用 return
这样才能对 x
和 y
:
生效
def name_check(x):
pattern1=r'[A-Za-z]'
if re.match(pattern1,x):
return x
else:
print ('This is not your name.')
return give_name()
def year_check(y):
pattern2=r'(\d)'
if re.match(pattern2,y):
return y
else:
print ('This is not your year of birth.')
return give_year()
def printing(x,y):
try:
print('Hey,',x,',in',int(y)+100,'year you will have 100 years.')
except:
print ('An error occured.')
def give_name():
x=str(input('Enter your name: '))
return name_check(x)
def give_year():
y=input('Enter your year of birth: ')
return year_check(y)
def program():
x=give_name()
y=give_year()
printing(x,y)
program()
我写了一个代码,这是一个简单的程序,有 2 个输入和打印。我添加了一些代码来防止名称和年份是正确的。当来自输入的数据正确时,我的程序可以正常工作,但当它不存在时,有相同的消息并且输入可以再次输入,但是在分配给变量新词之后,打印旧词:/我使用 Python 3.7.3
import re
def name_check(x):
pattern1=r'[A-Za-z]'
if re.match(pattern1,x):
pass
else:
print ('This is not your name.')
give_name()
def year_check(y):
pattern2=r'(\d)'
if re.match(pattern2,y):
pass
else:
print ('This is not your year of birth.')
give_year()
def printing(x,y):
try:
print('Hey,',x,',in',int(y)+100,'year you will have 100 years.')
except:
print ('An error occured.')
def give_name():
x=str(input('Enter your name: '))
name_check(x)
return x
def give_year():
y=input('Enter your year of birth: ')
year_check(y)
return y
def program():
x=give_name()
y=give_year()
printing(x,y)
program()
问题是您只在第一次捕获变量(x 和 y)。 试试这个:
import re
def name_check(x):
pattern1=r'[A-Za-z]'
if re.match(pattern1,x):
return True
else:
print ('This is not your name.')
return False
def year_check(y):
pattern2=r'(\d)'
if re.match(pattern2,y):
return True
else:
print ('This is not your year of birth.')
return False
def printing(x,y):
print(x,y)
try:
print('Hey,',x,',in',int(y)+100,'year you will have 100 years.')
except:
print ('An error occured.')
def give_name():
x=str(input('Enter your name: '))
while not name_check(x):
x=str(input('Enter your name: '))
return x
def give_year():
y=input('Enter your year of birth: ')
while not year_check(y):
y=input('Enter your year of birth: ')
return y
def program():
x=give_name()
y=give_year()
printing(x,y)
program()
在您的程序中,x
和 y
在链函数调用后不会改变。你应该在你的 year_check
和 name_check
函数中使用 return
这样才能对 x
和 y
:
def name_check(x):
pattern1=r'[A-Za-z]'
if re.match(pattern1,x):
return x
else:
print ('This is not your name.')
return give_name()
def year_check(y):
pattern2=r'(\d)'
if re.match(pattern2,y):
return y
else:
print ('This is not your year of birth.')
return give_year()
def printing(x,y):
try:
print('Hey,',x,',in',int(y)+100,'year you will have 100 years.')
except:
print ('An error occured.')
def give_name():
x=str(input('Enter your name: '))
return name_check(x)
def give_year():
y=input('Enter your year of birth: ')
return year_check(y)
def program():
x=give_name()
y=give_year()
printing(x,y)
program()