2016-12-01 90 views
1

我的脚本的目标是从关机前获取用户输入的时间以及关机过程中想要显示的消息。我的问题是我无法弄清楚如何将变量放入shutdown命令并让它正确执行。在关机命令中使用变量

import os 

time = (input("How much time till shutdown?")) 

message = input("What is your shutdown message?") 

shutdown = "shutdown /f /r /t", time "c", message 

os.system(shutdown) 
+0

是代码? – 2016-12-01 02:04:59

+0

是的,那是代码。 – BrewCrew15

回答

1

你需要(通过连接)来组装字符串shutdown以便它匹配你想要什么,包括周围的意见引号。

为此目的,在连接中使用的字符串文字使用单引号是很好的,这样可以在字符串内部自由使用非转义的双引号。

喜欢的东西:

time = input("How much time till shutdown? ") 
message = input("What is your shutdown message? ") 

shutdown = 'shutdown /f /r /t ' + time + ' /c "' + message +'"' 

print(shutdown) 

一个典型的运行:

How much time till shutdown? 60 
What is your shutdown message? Goodbye 
shutdown /f /r /t 60 /c "Goodbye"