Niku
Niku

Reputation: 91

Python OS open program closes immediately

I want to make a program which simulates a virus. (Just for trolling)
I have following code:

import random
import os

user_input = int(input("Your number between 0 and 100: "))

if user_input >= 50:
    random_number = random.randint(user_input, 100)
elif user_input <= 49:
    random_number = random.randint(0, user_input)

if user_input <= 100 and user_input >= 0:
    if user_input == random_number:
        print("Luckly")
        print(user_input)
        print(random_number)
    else:
        print("All your programs will be shut down!")
        print(user_input)
        print(random_number)
        shut_down_file = open("shut_down_script.py", "w")
        shut_down_file.write("print('You got hacked!')accept_task = input('Your PC will be shut down! (Press any key): ')")
        shut_down_file.close()
        os.startfile("shut_down_script.py")


I get no error, but it does not work. The shut_down_script program opens and closes directly although i have editet the code, that the user have to input anything

Upvotes: 0

Views: 175

Answers (1)

Henrik
Henrik

Reputation: 928

It is very simple. You have a python file named shut_down_script.py. In this script the code is like this:

print('You got hacked!')accept_task = input('Your PC will be shut down! (Press any key): ')

This wont work. Instead of this you have to put a \n. This makes a new line. So your code has to look like this:


import random
import os

user_input = int(input("Your number between 0 and 100: "))
    
if user_input >= 50:
    random_number = random.randint(user_input, 100)
elif user_input <= 49:
    random_number = random.randint(0, user_input)
    
if user_input <= 100 and user_input >= 0:
   if user_input == random_number:
        print("Luckly")
        print(user_input)
        print(random_number)
    else:
        print("All your programs will be shut down!")
        print(user_input)
        print(random_number)
        shut_down_file = open("shut_down_script.py", "w")
        shut_down_file.write("print('You got hacked!')\naccept_task = input('Your PC will be shut down! (Press any key): ')")
        shut_down_file.close()
        os.startfile("shut_down_script.py")

So between the ...got hacked!') and accept_task... a \n. Like this: print('You got hacked!')\naccept_task = input('Your PC will be shut down! (Press any key):

Upvotes: 2

Related Questions