简体   繁体   English

我需要添加什么,让我的Python 3程序检查输入是升序还是降序?

[英]What do I need to add, to ask my Python 3 program to check if the input is in ascending or descending order?

I need to make my program to check whether the input is in ascending order on in descending order. 我需要使我的程序检查输入是否按升序排列。 It works already, however not correctly, I believe I need to add something more? 它已经可以运行,但是不能正确运行,我相信我还需要添加更多内容吗? I am very new to programming. 我对编程非常陌生。

Here is my code : 这是我的代码:

b = 0
last = int(input()) 

finished= False
while not finished:
  new = int(input())
  if new == -1:
    finished = True
  elif last == -1:
    finished = True
  elif new > last :
    b = 1
  elif new <= last:
    b = 2
  last = new

if b == 1:
  print ('yes')
elif b == 2:
  print ('no') 

You should consider the values of b : 您应该考虑b的值:

b = 0
last = int(input()) 

while True:
    new = int(input())
    if (new == -1) or (last == -1):
        break
    elif new > last :
        if b == 2: # this has been descending until now
            b = 0 # neither ascending nor descending
            break
        b = 1
    elif new < last:
        if b == 1: # this has been ascending until now
            b = 0
            break
        b = 2
    else: # when two adjacent values are equal, this order is neither ascending, nor descending
        b = 0
        break
    last = new

if not b:
    print("Neither ascending, nor descending")
elif b == 1:
    print ('ascending')
elif b == 2:
    print ('descending')
else:
    print("This is odd, we shouldn't have got here...")

This will continue asking the user for inputs until it receives a -1 as input. 这将继续询问用户输入,直到收到-1作为输入为止。 If there's a single input that makes the list in descending order, it will print 'no' at the end of the loop (when the user inputs -1 ). 如果只有一个输入使列表降序排列,则它将在循环末尾(当用户输入-1 )打印'no'

b = 0
last = int(input('Last: ')) 

while True:
  new = int(input('New: '))     
  if new == -1 or last == -1:
      break
  elif new <= last:
    b = 2
  last = new

if b == 2:
  print ('no')
else:
  print ('yes')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3中的升序和降序 - Ascending and descending order in Python 3 需要帮助修复不正确的代码以询问用户是否要按升序或降序打印数字 - Need help fixing Incorrect code to ask the user user if they would like to print the numbers in ascending or descending order 如何检查排序和旋转的数组是升序还是降序? - How can I check if sorted and rotated array is in ascending or descending order? 如何按升序/降序对 tweepy 在 Python 中返回的解析不佳的 JSON 数据进行排序? - How do I sort in ascending/descending order this poorly parsed JSON data returned by tweepy in Python? Python:如何将文本文件中的整数按升序和/或降序排序? - Python: How do i sort integers in a text file into ascending and/or descending order? 如何在我的 python 脚本中重新启动 main() function? 无论我添加什么输入,程序都会重新启动 - How do I restart the main() function in my python script? The program restarts no matter what input I add Python-如何让我的程序反复要求输入,但仍存储其他输入? - Python - How do I get my program to ask for an input repeatedly but still store the other inputs? Python:我需要按降序排列这个列表 - Python: I need to sort this list in descending order 将 output 中的 Python 改为升序而不是降序 - Change the output to ascending instead of descending order in Python 冒泡排序帮助python - 升序和降序 - Bubble Sort help in python - ascending & descending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM