Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Why does this If Else statement not working in Python?

RAH S

Coder
I have the following code

[CODE lang="python" title="If Else Statement"] randomNum = random.randint(1,10)

number = input("To enter the land you must guess my number in 3 guesses or be taken to the darklands:
")
guesses = 1

while guesses != 3:
print()
guesses = guesses + 1

number = int(number)
if number < randomNum:
number = input("WRONG, my number is larger, try again: ")

elif number > randomNum:
number = input("WRONG, my number is smaller, try again: ")

elif number == randomNum:
break[/CODE]

but when I run it sometimes it works incorrectly as below


I am thinking of a number that is in between 1 and 10
To enter the land you must guess my number in 3 guesses or be taken to the darklands: 5

WRONG, my number is smaller, try again: 3

WRONG, my number is larger, try again: 4
You did not get it in time and will be sent to the darklands
 
Hi @RAH S,
I cannot help you if you do not include the whole code. You have not included the part where it outputs 'You did not get it in time and will be sent to the darklands.' Once you have sent the whole code, notify me and i will check it for errors.
 
@LTomy, I'm afraid that the first input is outside the loop, at line 3. So the loop has to be ran only 2 times. From what is provided, break line should be indented one time to the right. Also, line 4 should be concatenated to the end of line 3.

Otherwise, Expalse's requirement sounds reasonable.
 
You can also make it more efficient by doing this with the first input:
Python:
number = int(input("To enter the land you must guess my number in 3 guesses or be taken to the darklands:"))
#Removes the need for the line 'number = int(number)'
 
You can also make it more efficient by doing this with the first input:
Python:
number = int(input("To enter the land you must guess my number in 3 guesses or be taken to the darklands:"))
#Removes the need for the line 'number = int(number)'
True, but would not he need to do it with each 'input'?
 
It is most likely a problem with when and how the 'You did not get it in time and will be sent to the darklands' is displayed on the console.
 
Last edited:
I've tried this but then it gives the user 4 tries instead of 3
Like @ivan.moony said, since there has been a guess before this variable was set, it needs to be equal to 1. It is most likely a problem with how and when 'You did not get it in time and will be sent to the darklands' is displayed on the console, so if you could send that part of the code which you didn't previously send, then we can check it for errors.
 
I have changed the code a bit:

[CODE lang="python" title="Whole code"]import turtle
import random
import time

def death():
print("You have lost to one the dungeons scourges and are now in the darklands")
time.sleep(3)
quit()

def dragon():
print()
print("You hear a roar coming from the tunnel behind where the Wizard is.")
print("Cautiously, you enter the tunnel.")
print()
print("You see a DRAGON")
print("The DRAGON says, 'You must answer my riddle to continue.'")

answer = input("My riddle is 'The more you take of me,the more you leave behind': ")
if answer == "footsteps" or "Footsteps":
print("You are correct")
print("You now get the magical stone of immortality")
elif answer != "footsteps" or "Footsteps":
death()

print("Welcome to the Dungeon")
print()
print("Today you must embark on an adventure past Dragons and Wizards")
time.sleep(3)
print("Your first challenge shall be to face the Wizard.......Here he comes")
time.sleep(2)
print()



print("Hello Adventurer, you seek to enter the lands behind me")
print("I am thinking of a number that is in between 1 and 10")

randomNum = random.randint(1, 10)
guesses = 1
number = int(input("Guess it in 3 guesses or be sent to the darkland: "))

while guesses != 3:
guesses = guesses + 1
if number > randomNum:
number = int(input("You guessed too high. Try Again: "))
elif number < randomNum:
number = int(input("You guessed too low. Try Again: "))
elif number == randomNum:
break

if number == randomNum:
dragon()
elif number != randomNum:
death()


turtle.speed(80)
turtle.color('turquoise')
turtle.bgcolor('black')

for x in range(100):
turtle.color('turquoise')
turtle.forward(x)
turtle.left(45)
turtle.forward(x)
turtle.left(45)
turtle.color('red')
turtle.forward(x*2)
turtle.left(45)
turtle.forward(x)
turtle.left(45)
[/CODE]
 
You seem to have changed it quite a bit. The problem is the same?
By the way, in the following:
Python:
while guesses != 3:
      guesses = guesses + 1
      if number > randomNum:
            number = int(input("You guessed too high. Try Again: "))
      elif number < randomNum:
            number = int(input("You guessed too low. Try Again: "))
      elif number == randomNum:
            break
You can change the last 'elif' by simply an 'else' statement.
Here also:
Python:
if number == randomNum:
      dragon()
elif number != randomNum:
      death()
 
The code looks like it should work, what is the issue exactly?
The Issue is still the same. If you say
5 - it says go lower
then 3 - it says go higher
and finally 4 - it says that the answer is four but you took too long even though it was only 3 guesses.

BTW - Setting guesses to 0 before the while loop gives you 4 guesses for some reason.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom