Comparing digits in an integer in Python

后端 未结 3 976
慢半拍i
慢半拍i 2021-01-16 12:48

Really need some help here. Super early in learning Python.

The goal is to take a number and see if the digits are in ascending order. What I have so far is:

3条回答
  •  借酒劲吻你
    2021-01-16 13:34

    You would need a loop.

    for example:

    a = int(input("Enter a 4-digit number: "))
    
    b = [int(i) for i in str(a)]
    
    
    def isAscending(b):
      #loop for as many digits in the array
      for x in range(0, len(b) - 1):
        # if the next number is less than the previous return false
        if b[x] > b[x+1]:
          return False
      #  did not fail so return true
      return True
    
    if isAscending(b):
      print ("the number is in ascending order")
    else:
      print ("the number is not in ascending order")
    

提交回复
热议问题