2017-05-23 48 views
0

入力が降順で昇順であるかどうかを確認するプログラムを作成する必要があります。それはすでに動作しますが、正しくはありませんが、何かを追加する必要があると私は信じていますか?私はプログラミングに非常に新しいです。入力が昇順か降順かをチェックするためにPython 3プログラムに質問するには、何を追加する必要がありますか?

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') 
+0

あなたが取得するいくつかの入力と出力を提供してくださいすることができますリストにすべての入力を入れて、そのリスト – Dwaxe

+0

上で動作するように簡単でしょうか?あなたが必要としているものを理解することは容易になります – Exprator

+0

なぜ2つの入力を同時に要求しますか?これを行うと、あなたはまた、最後にデータを書き換えています。 – ForceBru

答えて

0

あなたは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...") 
+0

ありがとうございました! –

0

これは、入力として-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') 
関連する問題