본문 바로가기
python

sequence of "non-decreasing" numbers

by sj0020 2020. 6. 12.
def main():
	print("Enter a sequence of non-decreasing numbers.")
    count = 0
    last_num = float(input('Enter num: '))
    cur_num = last_num
    while cur_num >= last_num:
    	count += 1  #this line can go anywhere in body(while)
        last_num = cur_num
        cur_num = float(input('Enter num: '))
   print("Thanks for playing")
   print("sequence length: " + str(count))

(Suggested 15 minutes)

Write a program that asks the user to enter a sequence of "non-decreasing" numbers one at a time. Numbers are non-decreasing if each number is greater than or equal to the last.

When the user enters a number which is smaller than their previously entered value, the program is over. Tell the user how long their sequence was.

Here is an example (values entered by the user are bolded and italicized):

Enter a sequence of non-decreasing numbers.

Enter num: -1

Enter num: 0

Enter num: 1

Enter num: 3.12

Enter num: 99

Enter num: 99

Enter num: 42

Thanks for playing! Sequence length: 6