0
對於Python來說我是新手,我想知道如何將輸入或輸出存儲在變量中。Python:將輸出存儲在變量中
在shell腳本,我可以使用這個命令的輸出變量存儲
Variable_name=$(Command)
那麼我就可以這樣使用它,例如:
start_num=$(input start number)
increment_num=$(input increment number)
i=$start_num;
while [[ i -le $increment_num ]] ;
do
這裏是我的Python代碼,當然不起作用:
first = int(input("Enter the starting number: "))
second = int(input("Increment it by: "))
def start(first):
return(first)
def increment(second):
return(second)
count = start
while count != 100:
count += increment
print(count)
我到了這裏,另一個練習,我寫道:
first = int(input("Enter first number: "))
second = int(input("Enter second number: "))
def adding(first, second):
return(first + second)
result = adding(first, second)
print(result)
def plustest(result):
return(result + 100)
result2 = plustest(result + 100)
print(result2)
我不喜歡什麼是我必須寫「加(第一,第二):」不是從DEF得到它直的兩倍。
def adding(first, second):
return(first + second)
result = adding(first, second)
print(result)
像
result="def adding(first, second):
return(first + second)"
print($result)
或
result=$(def adding(first, second):
return(first + second))
print($result)
我不想重塑的Python,但我不知道如何實現我可以很容易地做shell腳本的蟒蛇。此外,如果有人會猜測什麼語言會接近Shell-Script和Python之間的混合。我習慣了Shell腳本,我喜歡使用pipe,grep,seg,paste等,但是我錯過了真正的編程語言的優點。希望它是有道理的。
乾杯。
如果我要存儲結果imput1和imput2這是非常好的。我的問題是,我有Input1和Imput2,我處理它的結果,然後我想採取結果和處理一些或打印它。所以我不能只打印第一和第二,因爲這是預處理它。我可以爲每個特定的案例編寫它,但我希望將輸出存儲在X中,然後再使用X. – benice
當您執行'result = first + second'時,該值存儲在'result'變量中。你可以在任何你想要的地方使用這個變量,然後改變它,例如'result = result + 1'。 – RetardedJoker
是否可以通過定位操作輸出而不是參數來將操作的輸出存儲到「結果」中?有點像這樣:結果=高清添加(第一,第二): 返回(第一+第二)打印(結果) – benice