【发布时间】:2018-03-12 19:26:18
【问题描述】:
我做了三个输入(名字、姓氏和年龄)并将它们放入一个文件中。最后一部分是如果年龄超过 18 岁,则使用此信息打印出某人的输入。我曾尝试使用字典,但不知何故我做错了。我可以使用列表或文件吗?我该怎么做才能使用使用该年龄的 if 语句来确定是否应打印详细信息。示例如下。
def details():
c = input('Press 1 to enter details, press 2 to browse users or press 3 to check age')
if c == '1':
firstname = input('Please enter your firstname')
surname = input('Please enter your surname')
age = input('Please enter your age')
myfile=open('details.txt', 'at')
myfile.write(firstname + '\n')
myfile.write(surname + '\n')
myfile.write(age + '\n')
myfile.close()
detail2 = {'Fname': firstname, 'Sname': surname, 'Age': age}
details()
elif c == '2':
detail2 = {'Fname': firstname, 'Sname': surname, 'Age': age}
read()
elif c == '3':
detail2 = {'Fname': firstname, 'Sname': surname, 'Age': age}
read18()
else:
details()
def read():
myfile=open('details.txt', 'rt')
x = myfile.read()
print(x)
def read18():
for item in detail2:
if Age> 18:
print('Over 18')
elif Age< 18:
print('Under 18')
"""I need to know what to do so this will print the details. Just using over or under 18 as a starting point"""
details()
【问题讨论】:
标签: python list file if-statement input