【发布时间】:2015-12-08 07:44:21
【问题描述】:
我创建了一个用作计数器的基本函数。但是,对于我通过脚本传递的每个参数,都需要使用 int() 将变量转换为整数。
“fun_loop(n, b)”中的两个变量都需要 int()。
from sys import argv
script, max_number, increment = argv
def fun_loop(n, b):
i = 0
numbers = []
while i < int(n):
print "At the top i is %d" % i
numbers.append(i)
i = i + int(b)
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
print "We can just give the numbers directly"
fun_loop(6, 1)
print "Or we can use variables from our script"
fun_loop(max_number, increment)
如果我在变量上运行没有 int() 的代码,那么我会得到...
一个无限循环 - 如果我传递变量 n 而没有 int()
或
TypeError: unsupported operand - 如果我传递变量 b 而没有 int()。
在传递每个变量时,我怎样才能使这个脚本工作而不必使用int()?
【问题讨论】:
标签: python string shell python-2.7 integer