我的代码为什么会执行我没有告诉它要执行的操作?

huangapple go评论96阅读模式
英文:

Why does my code do stuff i dont tell it to do?

问题

I'm new to coding so I don't get why my code isn't functioning. I want to have the variable com with input() as a line to write commands in and if the command is for example "delete file", I ask for a file name and it should delete the file. But if I type "delete file" it just shows me this:

  1. Command: delete file
  2. File Name: tester.txt
  3. Contents:
  4. testfile 123 test
  5. File name:

It just shows the contents of the file instead of deleting it. It should only read the contents if com is "open file". I hope you get my problem.

Thats the code:

  1. com = input("Command: ")
  2. if com == "open file" or com == "openfile":
  3. file = input("File Name: ")
  4. f = open(file, "r")
  5. print("Contents: ")
  6. print(f.read())
  7. if com == "delete file" or com == "deletefile":
  8. filefordelete = input("File name: ")
  9. if os.path.exists(filefordelete):
  10. os.remove(filefordelete)
  11. else:
  12. print("File does not exist!")
  13. input("Press enter to stop the program")

I want to delete the file when I type into the variable com "delete file". I cant find a solution why it's doing the stuff it should do when the variable com is "open file".

英文:

I'm new to coding so I don't get why my code isn't functioning. I want to have the variable com with input() as a line to write commands in and if the command is for example "delete file", I ask for a file name and it should delete the file. But if I type "delete file" it just shows me this:

  1. Command: delete file
  2. File Name: tester.txt
  3. Contents:
  4. testfile 123 test
  5. File name:

It just shows the contents of the file instead of deleting it. It should only read the contents if com is "open file". I hope you get my problem.

Thats the code:

  1. com = input("Command: ")
  2. if com == "open file" or "openfile":
  3. file = input("File Name: ")
  4. f = open(file, "r")
  5. print("Contents: ")
  6. print(f.read())
  7. if com == "delete file" or "deletefile":
  8. filefordelete = input("File name: ")
  9. if os.path.exists(filefordelete):
  10. os.remove(filefordelete)
  11. else:
  12. print("File does not exist!")
  13. input("Press enter to stop the program")

I want to delete the file when I type into the variable com "delete file". I cant find a solution why it's doing the stuff it should do when the variable com is "open file".

答案1

得分: 4

if com == "open file" or "openfile": 评估为

  1. if False or "openfile"

"openfile" 在 if 语句中评估为 True 值。
由于这是一个 or 条件 - False or True 评估为 True,因此你的第一个 if 语句为 True,并且执行相应的代码块。

你需要将你的条件重写为以下之一:

  1. if com == "open file" or com == "openfile":

或者

  1. if com in ["open file", "openfile"]:
英文:

if com == "open file" or "openfile": evaluates to

  1. if False or "openfile"

"openfile" evaluates to True value in the if statement.
And now since it’s an or condition - False or True evaluates to True, therefore your first if statement is True and the block is executed.

You will need to rewrite your condition like this:

  1. if com == "open file" or com == "openfile":

or

  1. if com in ["open file", "openfile"]:

huangapple
  • 本文由 发表于 2023年6月8日 16:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定