如何实现Python变量状态持久化,重启时校验并阻止代码运行?
Got it, let's fix this up for you. The core idea is to save the VAC flag to a file when the ban is triggered, then check that file as soon as the script starts to exit immediately if the flag is set. We'll use JSON for this since it's clean and easy to work with for simple key-value data.
Step 1: Add Startup Check for VAC State
First, we'll add code at the very top of your script to check if the user is already "banned" before running any other logic. We'll handle the case where the file doesn't exist (first run) gracefully.
Step 2: Save VAC State When Ban is Triggered
Then, we'll add code right after that Steam privilege suspension message to write the VAC=True state to a file.
Here's the full modified code:
import time import json # Define the file to store VAC status VAC_STATUS_FILE = "vac_ban_state.json" def check_vac_ban_on_startup(): try: # Try to read the existing ban state with open(VAC_STATUS_FILE, "r") as f: ban_data = json.load(f) if ban_data.get("VAC", False): print("You're already VAC banned! Cannot start the program.") exit() except FileNotFoundError: # File doesn't exist yet - no ban, proceed normally pass # Run the check immediately when the script starts check_vac_ban_on_startup() # Your existing code starts here (added input for context) Flashlight = input("Do you have a flashlight? (Y/N): ") if Flashlight in ['N', 'n', 'No', 'no', 'NO']: print("You can not proceed without a flashlight") time.sleep(1) print("sv_cheats 1") time.sleep(1) achievements = 'Off' print("Cheats are now enabled achievements will not be counted") time.sleep(1) print("prop_spawn Flashlight [@A] 1") time.sleep(1) print("Give [@P] Flashlight") time.sleep(1) print("sv_che...") time.sleep(3) print("-DISCONNECTED- VAC banned from secure server.") time.sleep(3) print("[xX_N00B5L4Y3R3OOO_Xx]") time.sleep(5) print("Your Steam Community privileges have been suspended permenantely for violations of the Steam Subscriber Agreement. Steam Community privileges will be restored Never. More information on this topic is available here: Steam-Community-FAQ") # Save the VAC ban state to file with open(VAC_STATUS_FILE, "w") as f: json.dump({"VAC": True}, f) time.sleep(2) print("Achievement: Banished from the Steam Community") exit()
Key Explanations:
- JSON Storage: We use JSON because it's human-readable, easy to parse, and avoids edge cases (like accidental whitespace breaking plain-text checks). It's perfect for storing simple state like this.
- Startup Check: The
check_vac_ban_on_startupfunction runs first. If thevac_ban_state.jsonfile exists and hasVAC: true, the script exits right away. If the file doesn't exist (first run), it proceeds normally. - Saving State: After printing the suspension message, we write the
{"VAC": True}object to the file. This ensures the next time the script runs, it detects the ban immediately.
If you ever want to reset the ban state, just delete the vac_ban_state.json file manually.
内容的提问来源于stack exchange,提问作者Taylor Roberts

