Making a collatz program automate the boring stuff

前端 未结 25 2145
一整个雨季
一整个雨季 2020-12-08 11:14

I\'m trying to write a Collatz program using the guidelines from a project found at the end of chapter 3 of Automate the Boring Stuff with Python. I\'m using python 3.

相关标签:
25条回答
  • 2020-12-08 11:53
    def collatz(number): 
        if number%2==0:    
            return number//2
        elif number%2==1:
            return number*3+1
    step=1 #counter variable for amusement and seeing how many steps for completion.
    try: #in case of ValueError   
        number=int(input('Enter a Number for Collatz Sequencing:')) 
        while collatz(number)!=1:    
            print(collatz(number))
            number=int(collatz(number))
            if collatz(number)!=1: 
                print('Calculating step ' + str(step) + '.')
                step=step+1
            else:
                print ('Calculating step ' +str(step) + '.')
                print('1 Has Been Reached.')
    except ValueError: 
         print('Enter an Integer please:')
    
    0 讨论(0)
提交回复
热议问题