[英]Python: Storing output in Variable
我是Python的新手,我想知道如何在變量中存儲輸入或輸出。
在Shell-Script中,我可以使用它將Command的輸出存儲在變量中
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)
我不喜歡的是我必須寫兩次“ adding(first,second):”,而不是直接從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,但我想知道如何實現在python的Shell-Script中可以輕松完成的工作。 同樣,如果有人會猜猜哪種語言會接近Shell-Script和Python之間的混合語言。 我習慣於Shell-Script,我喜歡使用管道,grep,seg,paste等,但是我卻缺少真正的編程語言的優勢。 希望有道理。
干杯。
下面的代碼用於接收輸入並對它們執行簡單的算術運算。
first = int(input("Enter first number: "))
輸入1
second = int(input("Enter second number: "))
輸入2
result = first + second
print result
>>> 3
print result + 1
>>> 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.