[英]I am trying to make a program but i dont know what im doing wrong
I am trying to make a program that asks the user a file input with the prompt "New file name" and the program should work like this and if the name has any form and/or combinations of "importantdocument.text" and i try the new forms and how can i make the code work easier than keep putting {variable} != {form of importantdocumant.txt}
can anyone help to see what i'm doing wrong or write a code that does what the program is supposed to do?我正在尝试制作一个程序,要求用户输入带有提示“新文件名”的文件,该程序应该像这样工作,如果名称具有“importantdocument.text”的任何形式和/或组合,我尝试新表单以及如何使代码工作比继续放置
{variable} != {form of importantdocumant.txt}
容易 任何人都可以帮助查看我做错了什么或编写代码来执行程序应该执行的操作? thanks ahead of time.提前谢谢。
my program我的程序
a = input('New file name: ')
if a != 'ImportantDocument.txt' and a != 'importantDOCUMENT.txt' and a != 'IMPORTANTDOCUMENT.TXT' and a != 'importantdocument.txt' and a != '':
print('That name is allowed.')
elif a == 'ImportantDocument.txt' or a == 'importantDOCUMENT.txt' or a == 'IMPORTANTDOCUMENT.TXT' or a == 'importantdocument.txt':
print('That file already exists.')
what the programs supposed to output程序应该输出什么
New file name: lessimportantdocument.txt
This name is allowed.
or或者
New file name: ImPorTaNtdoCumenT.tXt
That file already exists.
a = input('New file name: ')
k = a.upper()
if k != 'IMPORTANTDOCUMENT.TXT':
print("That name is allowed")
if k == 'IMPORTANTDOCUMENT.TXT':
print("That file already exists")
If you are wanting to check if the file exists, you could try read it.如果您想检查文件是否存在,您可以尝试阅读它。 I've not tested this but it should work.
我没有测试过这个,但它应该可以工作。
file_location = 'C:/Folder/' + a
try:
with open(file_location , "r") as f:
f.read()
except IOError:
print("That file already exists")
else:
print("That name is allowed")
If you literally just wanting to neaten up the code you have currently, then Mohd's answer is good.如果您实际上只是想整理您当前拥有的代码,那么 Mohd 的回答很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.