簡體   English   中英

我正在嘗試制作一個程序,但我不知道我做錯了什么

[英]I am trying to make a program but i dont know what im doing wrong

我正在嘗試制作一個程序,要求用戶輸入帶有提示“新文件名”的文件,該程序應該像這樣工作,如果名稱具有“importantdocument.text”的任何形式和/或組合,我嘗試新表單以及如何使代碼工作比繼續放置{variable} != {form of importantdocumant.txt}容易 任何人都可以幫助查看我做錯了什么或編寫代碼來執行程序應該執行的操作? 提前謝謝。

我的程序

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.')

程序應該輸出什么

New file name: lessimportantdocument.txt
This name is allowed.

或者

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")

如果您想檢查文件是否存在,您可以嘗試閱讀它。 我沒有測試過這個,但它應該可以工作。

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")

如果您實際上只是想整理您當前擁有的代碼,那么 Mohd 的回答很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM