ATBSWP Chapter 3 Practice Project - Wits end after 6 hours, yeah tell me about it

I have Youtubed,searched stack overflow and reddit for the the question and answer. Which I have found, but don't want to copy and paste the code just to get the answer but want to understand it.

Question and Answers: Making a collatz program automate the boring stuff (What I have learnt, which I thought was B.S. There are thousands of ways to answer a question with little to lots of code, surprising)

#Part 1 of The Collatz Sequence Chapter 3 practice project.

def collatz(number): #Defining the collatz function. 
    if number % 2 == 0:#If number is even use (number % 2 == 0)
        print(number // 2)
        return (number // 2) #What to return/print if number is even.
    elif number % 2 == 1: #If number is odd use (number % 2 == 1)
        print(3 * number + 1)
        return (3 * number + 1) #What to return/print if number is odd.

#Part 2 and 3 of The Collatz Sequence Chapter 3 practice project.

print('Enter number:') #Asking the person to enter a number.
while True:
    try: #To avoide non number erros. 
        number = int(input()) #Persons input.
    except (NameError, ValueError):#To ignore the errors that occur from strings and other non integers.
        print('Enter an integer:')#If person does not enter a number, ask them to.
      if number == 1:
          break
        print('Collatz Sequence Destination Achiveid')

(Yes, there are a lot of comments, but that was just to understand where I was effing up).

From my interpretation of code. I am screwing up where I am under section 2. I am having difficulty linking the user input into Collatz and the while loop but believe I have already done with "number = int(input())?. My code is also not initiating the Collatz sequence I have no idea why? maybe it's not linking to it. I also want to have it, if the person enters 1, it prints xyz after break and have used break to exist the loop if 1 is entered.

Am what I am doing right? I really liked this book upto this point and it's usually highly recommended. I have also read many Reddit threads were people have recommend other books such as python crash course. Would that book be better?. I don't think I am that dumb and understand the concepts and know how use them but this practice project is making me feel otherwise and many others have mentioned the same on threads.

*To whom ever is answering this, please try to nuke it down for me and where I went wrong or be open for me to ask question, thanks.

1 answer

  • answered 2021-05-17 05:36 Amp

    1. My code is also not initiating the Collatz sequence I have no idea why?

    You don't actually call collatz in your code, so your program just prints out the number that the user inputs, and ends.

    Next, your indentation is wrong. Inside the while True loop, your if block is not aligned with the other blocks.

    1. I also want to have it, if the person enters 1, it prints xyz after break and have used break to exist the loop if 1 is entered.

    For actual code, here it is. For the explanation, scroll further

    #Part 1 of The Collatz Sequence Chapter 3 practice project.
    
    def collatz(number): #Defining the collatz function. 
        if number % 2 == 0:#If number is even use (number % 2 == 0)
            print(number // 2)
            return (number // 2) #What to return/print if number is even.
        elif number % 2 == 1: #If number is odd use (number % 2 == 1)
            print(3 * number + 1)
            return (3 * number + 1) #What to return/print if number is odd.
    
    #Part 2 and 3 of The Collatz Sequence Chapter 3 practice project.
    
    print('Enter number:') 
    while True:
        try: 
            number = int(input())
        except (NameError, ValueError):
            print('Enter an integer:')
            continue
    
        if number == 1:
            print("xyz")
        break
    
    while number != 1:
        number = collatz(number)
    
    print('Collatz Sequence Destination Achiveid')
    

    How I figured it out

    Firstly, we have 3 parts

    1. Get the input from the user
    2. Making sure the input is valid. If not, prompt the user again
    3. Actually performing the collatz sequence

    1. Getting the input from the user

    That is as simple as

    number = int(input())
    

    2. Making sure the input is valid. If not, prompt the user again

    Now, we know that if the user does not enter a number, python will throw an error when trying to perform int(input()). We also know that we want to repeatedly prompt again and again if the user gives us invalid input, hence we should realise that a loop has to be used. Now, which loop do we use? A while or for loop?

    Usually, if we don't know how many times we want to loop, we will use a while. If we know, we will use a for. In this case, we don't know how many times the user might input something that is not valid, hence we will use a while loop. Basically, while the user does not give us a valid input, we ask him for an input.

    So first, let us include a loop.

    while True:
        number = int(input())
        
        if number == 1:
            print("xyz")
        break
    

    This still does exactly the same thing as before. We have not added any error checking yet. However, we did add the check that if number == 1, we print xyz. We prepare this loop so that we can reprompt the user again if the number fails. Now for the actual error checking,

    while True:
        try:
            number = int(input())
        except (NameError, ValueError): non integers
            print('Enter an integer:')
            continue
        
        if number == 1:
            print("xyz")
        break
    

    This is your code, with the addition of the number==1 check, and a continue keyword. When python encounters the continue keyword, it will immediately go into the next iteration of the loop. This means that, inside the except block, it will go back up to the top of the loop again, skipping the rest of the loop. This is how you perform the reprompt.

    The reason why I put a continue there is because I know that if the user gives me an incorrect input, I should immediately reject it, and reprompt the user once again. I don't have to do any additional checks.

    Try to trace the path your code takes if the input is

    1. a number that is not 1
    2. the number 1
    3. not a number

    3. Actually performing collatz

    Now that we have gotten the input from the user, we know that once we exit the loop, number is a valid number. Now we actually perform collatz.

    Basically, while the number is not 1, we will have to continue calling collatz. Hence

    while number != 1:
        number = collatz(number)
    
    print('Collatz Sequence Destination Achiveid')