【问题标题】:How do i write an algorithm that reads three numbers and prints them out in ascending order in python我如何编写一个算法来读取三个数字并在python中按升序打印出来
【发布时间】:2016-11-06 21:36:05
【问题描述】:

我如何编写一个算法来读取三个数字并在 python 中按升序打印出来。请帮助这是我迄今为止尝试过的,希望你们能帮助我,谢谢,这是使用python,我是编程新手。我不知道如何做它所说的升序部分。

one = float(input("Please input a number : "))
two = float(input("Please input a second number : "))
three = float(input("Please input a third number : "))

if one > two and three:
    print("")*

【问题讨论】:

  • sorted([one, two, three])
  • 数字按从小到大排列的顺序称为升序。例如。图5、9、13、17和21按升序排列。 from www.mathsteacher.com.au如果你在上101编程课程,我认为问题与条件有关。您应该检查if...else 语句。 Check this page
  • @idjaw 你能解释一下怎么做吗?没看懂
  • @JoseRaulBarreras 我知道这意味着什么,但我不知道如何在 python 中做到这一点
  • 你的意思是if one > two > three:。放下and

标签: python if-statement iif-function


【解决方案1】:

你可以使用 sorted(),但首先你需要将数字保存在一个列表中,试试这个:

nums = []
one = nums.append(float(input("Please input a number : ")))
two = nums.append(float(input("Please input a second number : ")))
three = nums.append(float(input("Please input a third number : ")))
for num in sorted(nums):
    print (num)

sorted() - Python Doc

如果您需要使用 if 和 else 语句,您应该考虑输入数字的所有组合并评估它,试试这个:

one = float(input("Please input a number : "))
two = float(input("Please input a second number : "))
three = float(input("Please input a third number : "))

if one < two and three < two:
    if one > three:
        print(three, one, two)
    else:
        print(one, three, two)
elif one < three and two < three:
    if one > two:
        print(two, one, three)
    else:
        print(one, two, three)        
elif two < one and three < one:
    if three > two:
        print(two, three, one)
    else:
        print(three, two, one)
else:
    print(one, two, three)

【讨论】:

  • 查看我发布的其他解决方案,您可以使用 if 和 else 语句
【解决方案2】:

根据您的代码,以下代码将按升序顺序打印数字一和二。我建议你尝试完成三个元素的代码。

one = float(input("Please input a number : "))
two = float(input("Please input a second number : "))
three = float(input("Please input a third number : "))

if one < two:
    print(one, two)
else:
    print(two, one)

你的举动...

【讨论】:

  • 三个数字怎么办?
猜你喜欢
  • 2015-11-24
  • 2022-10-24
  • 2023-01-21
  • 2017-04-24
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
相关资源
最近更新 更多