Home > other >  I'm trying to create a simple loop but I'm stuck on line 18 it seems
I'm trying to create a simple loop but I'm stuck on line 18 it seems

Time:03-10

secret_number = 777

print(
"""
 ================================ 
| Welcome to my game, muggle!    |
| Enter an integer number        |
| and guess what number I've     |
| picked for you.                |
| So, what is the secret number? |
 ================================ 
""")

    guess_number = int(input("What is the secret number?"))
#if number is not equal to 777 continue
    while guess_number != secret_number:
    
    If guess_number == secret_number:
        guess_number = secret_number
    #Input the next number
    guess_number = int(input("What is the secret number?"))

print("Ha ha! You're stuck in my loop!")
print("Well done, muggle! You are free now.")

CodePudding user response:

You have several syntax errors, the one you are seeing now is incorrect indentation. You need to start at the begining on the line.

Solved:

secret_number = 777

print(
"""
 ================================ 
| Welcome to my game, muggle!    |
| Enter an integer number        |
| and guess what number I've     |
| picked for you.                |
| So, what is the secret number? |
 ================================ 
""")

guess_number = int(input("What is the secret number?"))
#if number is not equal to 777 continue
while guess_number != secret_number:

    #if guess_number == secret_number:
        # guess_number = secret_number
        # note that the previous line is unnecessary
    #Input the next number
    guess_number = int(input("What is the secret number?"))

    print("Ha ha! You're stuck in my loop!")
    #this should be at the top, otherwise it will be printed even 
    #if you guess correctly the first time
print("Well done, muggle! You are free now.")

CodePudding user response:

apart from the If which should be if and the indentation problems, your code has parts that are not needed.

You don't need to check if they are equal inside the loop, since you are already checking that in the while condition.

Try something like this:

secret_number = 777

print(
"""
 ================================ 
| Welcome to my game, muggle!    |
| Enter an integer number        |
| and guess what number I've     |
| picked for you.                |
| So, what is the secret number? |
 ================================ 
""")

# guess the number for the first time
guess_number = int(input("What is the secret number?"))

#if number is not equal to 777 enter the loop
while guess_number != secret_number:
    print("Ha ha! You're stuck in my loop!")
    # Input the next number
    guess_number = int(input("What is the secret number?"))

print("Well done, muggle! You are free now.")
  • Related