2016-12-01 94 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"