简体   繁体   English

我正在尝试制作一个程序,但我不知道我做错了什么

[英]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.

相关问题 我正在尝试制作一个程序来跟踪用户提供的字符串中最常见的字符。 我在这里做错了什么? - I am trying to make a program that tracts the most frequent character in a string presented by a user. What am I doing wrong here? 我正在尝试使用pycrypto了解AES,但我不知道自己在做什么错 - I am trying to use pycrypto to learn about AES and I do not know what I am doing wrong 我正在尝试在 python 中创建一个密码系统。 我不知道这有什么问题 - i am trying to make a password system in python. i dont know whats wrong with this 我很想制作 Alexa 技能,但它不适用于 API。 我在做什么错(python) - Im tyring to make an Alexa skill and its not working with the API. What am i doing wrong (python) 递归程序:我在做什么错? - Recursive Program: What am I doing wrong? 我正在尝试使用记事本++启动一个简单的计算器程序,我做错了什么? - I am trying to start a simple calculator program using notepad++, what am I doing wrong? 我试图使这个数组中的偶数等于0,我做错了什么? - I am trying to make even numbers in this array equal 0, what am I doing wrong? 我正在尝试用 python 制作一个 bmi 计算器,但我不知道我做错了什么 - I'm trying to make a bmi calculator in python and I don't know what I'm doing wrong 我试图找出 .csv 文件的最大值和最小值,但我无法弄清楚我做错了什么 - Im trying to figure out the max and min of a .csv file and i cant figure out what im doing wrong 我得到错误的python输出格式。 我究竟做错了什么? - Im getting wrong python output format. what am i doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM