【发布时间】:2019-04-27 14:36:32
【问题描述】:
我写了一个代码,这是一个简单的程序,有 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()
【问题讨论】:
-
请提供不适合您的输入!
-
尝试在
def printing(x,y):中将print (y,type(y))放在print ('An error occurred.')之前。然后您将确定问题所在
标签: python variables error-checking attribution