Loop error in my number guess program

I tried to make this easy program using random and loops. I am really bad in loops. The random number generation worked perfectly but its was not able to loop through this program. Here is the program

print("You will have to guess the number chosen by the computer.The computer will choose a number from 1 to 10")
User_Input = int(input("Make your guess: "))

if User_Input > 10:
 print("Your guess is more than 10, Please choose between 1 and 10")
else:
  print("The computer is choosing......")

    
Computer_List = ("1","2","3","4","5","6","7","8","9","10")

import random

Chosen_Number = random.choice(Computer_List)

for Calculation in Chosen_Number:
  if User_Input < Chosen_Number:
    print("The number you have chosen is more than the number chosen by the computer. Try again!")
  elif User_Input > Chosen_Number:
    print("The number you have chosen is less than the number chosen by the computer. Try again!")
  elif User_Input == Chosen_Number:
    print("The number chosen by you is CORRECT!")
  else:
    print("Some error occured try again")
  break

Please don’t send a whole new and modified program as I don’t want to copy but I want to learn, just help me on what’s the error, what made this error occur and how to fix it.

Thanks for all the people who replied to this help request

Please check and refine the indentation, so that execution does not always break out of the loop.

Ok sure as I am beginner could you please tell me how to do that it will help a lot!

Notice the very last line of code. That needs to be in a conditional block, so that the loop does not always terminate during the first iteration.

EDIT (March 2, 2022):

For example, you have this:

  else:
    print("Some error occured try again")
  break

Instead, you could place the break in a conditional block, as follows:

  else:
    print("Some error occured try again")
    break

ohh like that so I have to put the break statement inside the else statement right?
Are there any other mistakes that are present in the program?

To answer that question, you should run the program several times to test it with various inputs.

EDIT (March 2, 2022):

If, when you run your program, you get an error message, please post the most recent version of your code, as well as a copy of the error message.

Also check the logic of the expressions in the headers of your conditional blocks to make sure they make sense, when compared to the associated print statements. For example, does the condition here match the printed message?:

  if User_Input < Chosen_Number:
    print("The number you have chosen is more than the number chosen by the computer. Try again!")

Yeah thanks for the help. I found out what was the problem. Thanks to you. There was some problem with indentation. Thanks for the help :grinning:

1 Like

Please make sure to test the program even after you are not receiving any error messages. A program may execute without an error message, but still not produce the desired result.

Make sure the break statement is in the correct conditional block. For instance, it might need to be moved to the block that executes when the computer has discovered the correct number.

1 Like

Yeah sure. The “break” line was making the problem. Thanks for the help!

1 Like