[英]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.