Discuss Scratch

enspcds
Scratcher
100+ posts

Help in Python?

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
Ghost Game
from random import randint
print ('Ghost Game')
feeling_brave = True
score = 0
while feeling_brave:
ghost_door = randint (1, 3)
print ('Three doors ahead…')
print ('A ghost behind one.')
print ('Which door do you open?')
door = input ('3, 2, or 1?')
door_num = int('door')
if door_num == ghost_door:
print ('GHOST!')
feeling_brave = false
else:
print ('No ghost!')
print ('You enter the next room!')
score = score + 1
print ('Game over. You scored', score)

BlueCrayfish
Scratcher
500+ posts

Help in Python?

Sorry, I only know Python 2, and not Python 3. Do you have an IDE with a Debugger or are you using an online compiler?
enspcds
Scratcher
100+ posts

Help in Python?

BlueCrayfish wrote:

Sorry, I only know Python 2, and not Python 3. Do you have an IDE with a Debugger or are you using an online compiler?
What do you mean?
BookOwl
Scratcher
1000+ posts

Help in Python?

enspcds wrote:

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
 Ghost Game 
from random import randint
print ('Ghost Game')
feeling_brave = True 
score = 0
while feeling_brave:
    ghost_door = randint (1, 3)
    print ('Three doors ahead...')
    print ('A ghost behind one.')
    print ('Which door do you open?')
    door = input ('3, 2, or 1?')
    door_num = int('door')
    if door_num == ghost_door:
        print ('GHOST!')
        feeling_brave = false
    else:
        print ('No ghost!')
        print ('You enter the next room!')
        score = score + 1
print ('Game over. You scored', score)
Here's my fixed version with comments explaining what I changed:
 
# Ghost Game # This needs to be a comment
from random import randint
print ('Ghost Game')
feeling_brave = True 
score = 0
while feeling_brave:
    ghost_door = randint (1, 3)
    print ('Three doors ahead...')
    print ('A ghost behind one.')
    print ('Which door do you open?')
    door = input ('3, 2, or 1?')
    door_num = int(door) # You need to use the door variable, NOT the string 'door'
    if door_num == ghost_door:
        print ('GHOST!')
        feeling_brave = false
    else:
        print ('No ghost!')
        print ('You enter the next room!')
        score = score + 1
print ('Game over. You scored', score)
 [\code]
Savio-5002
Scratcher
45 posts

Help in Python?

enspcds wrote:

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
Ghost Game
from random import randint
print ('Ghost Game')
feeling_brave = True
score = 0
while feeling_brave:
ghost_door = randint (1, 3)
print ('Three doors ahead…')
print ('A ghost behind one.')
print ('Which door do you open?')
door = input ('3, 2, or 1?')
door_num = int('door')
if door_num == ghost_door:
print ('GHOST!')
feeling_brave = false
else:
print ('No ghost!')
print ('You enter the next room!')
score = score + 1
print ('Game over. You scored', score)

Hey I read that book!
There's a space in between randint and the numbers on the
 ghost_door = randint (1,3)
^
line.
And there needs to be hashtags on the first line, or you'll get a NameError. Can you specify the error, if that did not fix it?(e.g. NameError, SyntaxError, TypeError, errno 10035)

Last edited by Savio-5002 (Aug. 27, 2016 20:16:45)

lugga
Scratcher
500+ posts

Help in Python?

BookOwl wrote:

enspcds wrote:

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
-snip-
Here's my fixed version with comments explaining what I changed:
 
# Ghost Game # This needs to be a comment
from random import randint
print ('Ghost Game')
feeling_brave = True 
score = 0
while feeling_brave:
    ghost_door = randint (1, 3)
    print ('Three doors ahead...')
    print ('A ghost behind one.')
    print ('Which door do you open?')
    door = input ('3, 2, or 1?')
    while not door in ['1', '2', '3']: # Makes sure you inputted a valid number and not an arbitrary string / number
        door_num = int(door) # You need to use the door variable, NOT the string 'door'
    if door_num == ghost_door:
        print ('GHOST!')
        feeling_brave = False
    else:
        print ('No ghost!')
        print ('You enter the next room!')
        score += 1 # Saves time when writing. It is the same as "score = score + 1"
print ('Game over. You scored', score)
 

Some more changes and explanations why.
(I only made 1 change and added another line
so that's 4 changes from the original in total)

Edit: Changed wording in one of the comments

Last edited by lugga (Aug. 24, 2016 15:44:15)

lugga
Scratcher
500+ posts

Help in Python?

Savio-5002 wrote:

enspcds wrote:

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
Ghost Game
from random import randint
print ('Ghost Game')
feeling_brave = True
score = 0
while feeling_brave:
ghost_door = randint (1, 3)
print ('Three doors ahead…')
print ('A ghost behind one.')
print ('Which door do you open?')
door = input ('3, 2, or 1?')
door_num = int('door')
if door_num == ghost_door:
print ('GHOST!')
feeling_brave = false
else:
print ('No ghost!')
print ('You enter the next room!')
score = score + 1
print ('Game over. You scored', score)

Hey I read that book!
There's a space in between randint and the numbers on the
 ghost_door = randint (1,3)
^
line.
And there needs to be hashtags on the first line, or you'll get a NameError. Can you specify the error, if that did not fix it?(e.g. NameError, SyntaxError, TypeError, errno 10035)


define Promote underrated OSes
Windows Vista rules (100000000000000000000000000000000000000000000000000000000000000000000)%
Windows Me rules (100) %
Windows 8 doesn't deserve any promotion
Neither does Windows 7
Or any other but<not<Windows Vista[because it rules]>>
say [YAY WINDOWS VISTA!!!!]
Hi please don't blockspam (adding unnecessary scratchblocks) if you want it as your signature you can add it there by going to the discussion home and at the bottom of the page and clicking “edit my signature”.
enspcds
Scratcher
100+ posts

Help in Python?

Savio-5002 wrote:

enspcds wrote:

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
Ghost Game
from random import randint
print ('Ghost Game')
feeling_brave = True
score = 0
while feeling_brave:
ghost_door = randint (1, 3)
print ('Three doors ahead…')
print ('A ghost behind one.')
print ('Which door do you open?')
door = input ('3, 2, or 1?')
door_num = int('door')
if door_num == ghost_door:
print ('GHOST!')
feeling_brave = false
else:
print ('No ghost!')
print ('You enter the next room!')
score = score + 1
print ('Game over. You scored', score)

Hey I read that book!
There's a space in between randint and the numbers on the
 ghost_door = randint (1,3)
^
line.
And there needs to be hashtags on the first line, or you'll get a NameError. Can you specify the error, if that did not fix it?(e.g. NameError, SyntaxError, TypeError, errno 10035)


define Promote underrated OSes
Windows Vista rules (100000000000000000000000000000000000000000000000000000000000000000000)%
Windows Me rules (100) %
Windows 8 doesn't deserve any promotion
Neither does Windows 7
Or any other but<not<Windows Vista[because it rules]>>
say [YAY WINDOWS VISTA!!!!]

Savio-5002 wrote:

enspcds wrote:

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
Ghost Game
from random import randint
print ('Ghost Game')
feeling_brave = True
score = 0
while feeling_brave:
ghost_door = randint (1, 3)
print ('Three doors ahead…')
print ('A ghost behind one.')
print ('Which door do you open?')
door = input ('3, 2, or 1?')
door_num = int('door')
if door_num == ghost_door:
print ('GHOST!')
feeling_brave = false
else:
print ('No ghost!')
print ('You enter the next room!')
score = score + 1
print ('Game over. You scored', score)

Hey I read that book!
There's a space in between randint and the numbers on the
 ghost_door = randint (1,3)
^
line.
And there needs to be hashtags on the first line, or you'll get a NameError. Can you specify the error, if that did not fix it?(e.g. NameError, SyntaxError, TypeError, errno 10035)


Cool! It was a syntax error, or something like that…
Savio-5002
Scratcher
45 posts

Help in Python?

lugga wrote:

Savio-5002 wrote:

enspcds wrote:

I just started using Python, and made a simple game and checked over everything but it has an error. Can anybody help?
Ghost Game
from random import randint
print ('Ghost Game')
feeling_brave = True
score = 0
while feeling_brave:
ghost_door = randint (1, 3)
print ('Three doors ahead…')
print ('A ghost behind one.')
print ('Which door do you open?')
door = input ('3, 2, or 1?')
door_num = int('door')
if door_num == ghost_door:
print ('GHOST!')
feeling_brave = false
else:
print ('No ghost!')
print ('You enter the next room!')
score = score + 1
print ('Game over. You scored', score)

Hey I read that book!
There's a space in between randint and the numbers on the
 ghost_door = randint (1,3)
^
line.
And there needs to be hashtags on the first line, or you'll get a NameError. Can you specify the error, if that did not fix it?(e.g. NameError, SyntaxError, TypeError, errno 10035)


-removed-
Hi please don't blockspam (adding unnecessary scratchblocks) if you want it as your signature you can add it there by going to the discussion home and at the bottom of the page and clicking “edit my signature”.
I wish you told me that earlier…

Last edited by Savio-5002 (Aug. 27, 2016 20:18:04)

enspcds
Scratcher
100+ posts

Help in Python?

Thanks everyone!

Powered by DjangoBB