如何交换初始数字和结束数字的值?

时间:2017-09-30 14:23:02

标签: python-3.x

我有这个程序:

print('Python accept decrease numbers\n')

initial = int(input('Initial number:'))
ending = int(input('Ending number:'))
decrease = -(int(input('Decrease number:')))

for n in range(initial,ending,decrease):
    if (initial < ending):
        ending,initial = initial,ending
    print (n)

如果初始值小于结束值,我想交换初始值和结束值,以便我可以得到减少值 相反,它什么都不打印出来:

Python accept decrease numbers

Initial number:5
Ending number:50
Decrease number:2
>>> 

我错过了什么吗? 感谢。

1 个答案:

答案 0 :(得分:0)

只需在for周期之外移动支票:

print('Python accept decrease numbers\n')

initial = int(input('Initial number:'))
ending = int(input('Ending number:'))
decrease = -(int(input('Decrease number:')))

if (initial < ending):
    ending,initial = initial,ending

for n in range(initial,ending,decrease):
    print (n)