Python Forum
Newbie to Python Why does this if statement not work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie to Python Why does this if statement not work?
#1
Hi

Im new to Python but have programmed in Basic many many years ago.

Im trying to help my daugther with her Python homework, and for the life of me i dont understand why this If statement doesnt evaluate properly.

Could someone generously provide some help into why this simple program doesnt print "Ghost" when bth ghost_door and door_num are the same values.

import random
print("Ghost Game")

feeling_brave=True

score=0

while feeling_brave == True:
    ghost_door=random.randint(1,3)
    print("Three doors ahead")
    print("A ghost behind one")
    print("Which door do you open?")
    door_num=input("1, 2, or 3? ")
    print("ghost door",ghost_door) #test
    print("Door num",door_num)     #test
    
    if ghost_door == door_num:
        print("Ghost!")
        feeling_brave = False
        
    else:
        print("No ghost!")
        print("You enter the next room.")
        score = score + 1
        
print("Run Away!")
print("Game over! You scored",score)
Thankyou in adavance.

Im sure its is something really simple but im stuck.

Best regards

Andy
Reply
#2
ghost_door is an int, but input() returns str, so they are never equal.
You need to make them the same type (either convert ghost_door to str, or door_num to int)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thankyou.

Can i define the input as an int?

what is the syntax?

Is it....

door_num=input.int("1, 2 or 3?")
or do i need to define door_num as an int first?

Best regards

Andy

Problem solved.

Code as follows.

import random
print("Ghost Game")

feeling_brave=True

score=0

while feeling_brave == True:
    ghost_door=random.randint(1,3)
    print("Three doors ahead")
    print("A ghost behind one")
    print("Which door do you open?")
    door_num=int(input("1, 2, or 3? "))
    
    if ghost_door == door_num:
        print("Ghost!")
        feeling_brave = False
        
    else:
        print("No ghost!")
        print("You enter the next room.")
        score = score + 1
        
print("Run Away!")
print("Game over! You scored",score)
Thankyou Buran
Reply
#4
note that feeling_brave is a boolean, so you can do just
while feeling_brave:

also, to remove the need of using a flag (feeling_brave) you can do:

import random
print("Ghost Game")
 
score=0
 
while True:
    ghost_door = random.randint(1,3)
    print("Three doors ahead")
    print("A ghost behind one")
    print("Which door do you open?")
    door_num = int(input("1, 2, or 3? "))
     
    if ghost_door == door_num:
        print("Ghost!")
        break # break out of the loop
         
    else:
        print("No ghost!")
        print("You enter the next room.")
        score = score + 1
         
print("Run Away!")
print("Game over! You scored",score)
But as it is homework, it may not be consistent with what they have learned in school yet.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Buran

Thankyou.

That is useful to know.

I think i might take up learning Python myself.

Quite enjoyed trying to solve the problem with help from your good self.

Thanks again.

Any advice on the best training site for Python such as a course?

Best regards
Andy
Reply
#6
No first hand experience with course, but:
https://python-forum.io/Thread-How-to-be...#pid108106

also the official docs:
the Language reference and the Library refernce

and of course there is the official Python tutorial and the Python HOWTOs

There are many thraeds here on forum, asking for advise. Also our Tutorial section and especially https://python-forum.io/Thread-A-List-of...-Resources
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Python newbie laleebee 2 1,286 May-24-2022, 01:39 PM
Last Post: laleebee
  getting an import statement to work in my program barryjo 1 1,616 Dec-06-2021, 04:28 PM
Last Post: snippsat
  Why doesn't this print statement work? stylingpat 10 5,594 Mar-23-2021, 07:54 PM
Last Post: buran
  Newbie on Python syntax rud0lp20 6 2,877 Apr-21-2020, 04:26 PM
Last Post: jefsummers
  return statement will not work TheTechRobo 2 2,588 Mar-30-2020, 06:22 PM
Last Post: TheTechRobo
  python newbie marcush929 2 2,561 Jan-01-2020, 08:06 AM
Last Post: marcush929
  why does this if-statement not work? sandeen 2 2,012 Nov-15-2019, 03:17 PM
Last Post: sandeen
Smile Help needed. Python Newbie!, it will be fun. knightdea 3 2,580 Oct-13-2019, 08:50 AM
Last Post: perfringo
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,156 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020