2017-09-04 156 views
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等,但是我错过了真正的编程语言的优点。希望它是有道理的。

干杯。

回答

0

下面的代码用于输入输入并对它们执行简单的算术运算。

first = int(input("Enter first number: ")) 

输入1个

second = int(input("Enter second number: ")) 

输入2

result = first + second 

print result 

>>> 3 

print result + 1 

>>> 4 
+0

如果我要存储结果imput1和imput2这是非常好的。我的问题是,我有Input1和Imput2,我处理它的结果,然后我想采取结果和处理一些或打印它。所以我不能只打印第一和第二,因为这是预处理它。我可以为每个特定的案例编写它,但我希望将输出存储在X中,然后再使用X. – benice

+0

当您执行'result = first + second'时,该值存储在'result'变量中。你可以在任何你想要的地方使用这个变量,然后改变它,例如'result = result + 1'。 – RetardedJoker

+0

是否可以通过定位操作输出而不是参数来将操作的输出存储到“结果”中?有点像这样:结果=高清添加(第一,第二): 返回(第一+第二)打印(结果) – benice

相关问题