'Change the value of an order
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.
Solution 1:[1]
Did you miss the f in f-string?
f"shutdown /s /t {ticks}"
Solution 2:[2]
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.
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rahul |
| Solution 2 | M.Mevlevi |
| Solution 3 | Ka-Wa Yip |
