Reputation: 25
I'm trying to change the value of a command on each run. For this I tried a prototype like this:
import os
import random
from random import random
class testShutDown :
ticks = random.randint(10,100)
os.system("shutdown /s /t {ticks}")
According to me, with each execution of the code, the computer must shut down randomly between 10 and 100 seconds.
Unfortunately, it doesn't work... I hope someone can help me solve this problem which seems simple to me, but not for me.
Upvotes: -1
Views: 69
Reputation: 3009
You have to add the f
for strings substitution. For example,
>>> name = "apple"
>>> f"My test name is {name}."
'My test name is apple.'
Therefore the solution is to modify the os
line to this:
os.system(f"shutdown /s /t {ticks}")
Alternatively, you can use the format
function:
os.system('shutdown /s /t {}'.format(ticks))
Also, you should just use import random
instead of from random import random
.
Upvotes: 0
Reputation: 349
import os
import random
from random import random
class testShutDown :
ticks = random.randint(10,100)
os.system(f"shutdown /s /t {ticks}")
i think u forgot to add f
before string try this.
f tells that the string contains a variable.
Upvotes: 0