python用while循环

img


a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a > b:
i = a
while i < b+1:
print(i,end=" ")
i += 1
elif a < b:
i = b
while i
print(i,end=" ")
i += 1
要求输入数字,然后按从大到小去排列数字,写出代码后输入数字,什么都输出不出来,也没有报错。

你题目的解答代码如下:

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a < b:   # 这里是 a < b
    i = a
    while i <= b:
        print(i,end=" ")
        i += 1
elif a >= b:  # 这里是 a >= b
    i = b
    while i <= a:
        print(i,end=" ")
        i += 1

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img


,

代码缺失,重新贴一下

img

截图是从小到大排列,你要的是从大到小排列?
从小到大排列

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a > b:
  i =b
  while i < a+1:
    print(i,end=" ")
  i += 1
elif a < b:
  i = a
  while i <b+1:
    print(i,end=" ")
  i += 1

从大到小排列

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a > b:
  i =a
  while i>b-1:
    print(i,end=" ")
  i -= 1
elif a < b:
  i = b
  while i >a-1:
    print(i,end=" ")
  i -= 1

可以看下python参考手册中的 python- 循环的技巧您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632