0

So I have written this code for this game, using a user input, but it does not seem to be working. This is my code.

import random

print ("Welcome to the hungry dog game.")
print ("The aim of this game is to save the hungry dog. How long can you keep it alive for?")


true = True
false = False
aliveordeadlist1 = [true, false]
random.choice(aliveordeadlist1)





feed1 = input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.")
if feed1 == "no" and aliveordeadlist1 == false:
    print ("Well you were wrong, he was hungry. Your dog died sorry. Better luck next time.")

if feed1 == "yes" and aliveordeadlist1 == false:
    print ("As I told you he wansn't hungry> You overfed him so hed died sorry. Better luck next time.");
if feed1 == "no" and aliveordeadlist1 == true:
    print ("Well done he wasn't hungry. He is still alive (for now...)");
if feed1 == "yes" and aliveordeadlist1 == true:
    print ("Well done he actually was hungry so he would have died if you didn't feed him.He is still alive (for now...)");

When I run this code in the shell and for example, type yes, it returns this error:

Traceback (most recent call last):
  File "/Users/mac/Documents/hungrydoggame.py", line 16, in <module>
    feed1 = input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.")
  File "<string>", line 1, in <module>
NameError: name 'yes' is not defined

I am unsure why I am receiving this error and what I should do to fix it, does anyone have any suggestions?

2
  • Work for me just fine. Commented Mar 18, 2017 at 19:02
  • 2
    use raw_input() Commented Mar 18, 2017 at 19:08

3 Answers 3

1

First of all if you're doing python2.7 program as mentioned here the input method should be used as input_raw so it will return you the response as string and in line7 you do a random choice for if the dog is dead or alive but never save it in the variable in which you will be chekcing aliveordead = random.choice(aliveordeadlist1). So here's the proper code.

import random

print ("Welcome to the hungry dog game.")
print ("The aim of this game is to save the hungry dog. How long can you keep it alive for?")


true = True
false = False
aliveordeadlist1 = [true, false]
aliveordead = random.choice(aliveordeadlist1)





feed1 = raw_input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.")

if feed1 == "no" and aliveordead == false:
    print ("Well you were wrong, he was hungry. Your dog died sorry. Better luck next time.")

if feed1 == "yes" and aliveordead == false:
    print ("As I told you he wansn't hungry> You overfed him so hed died sorry. Better luck next time.");
if feed1 == "no" and aliveordead == true:
    print ("Well done he wasn't hungry. He is still alive (for now...)");
if feed1 == "yes" and aliveordead == true:
    print ("Well done he actually was hungry so he would have died if you didn't feed him.He is still alive (for now...)");
Sign up to request clarification or add additional context in comments.

Comments

0

I don't get the error that you get, but you need to set the random.choice call to a variable, and then reference that in your if statements (and not aliveordeadlist1), otherwise you're comparing your list to the single word 'yes' or 'no' and that will always return False.

Comments

0

This code has some serious issues which I imagine may lead to unexpected behaviour in some versions of Python:

  • Indentation is relevant in Python. If you really have indented all the code after the first line I don't even understand why your code runs at all.
  • You define aliases for True and False. This may not be a problem, but I don't know whether true and false are reserved words.

There is also a logical issue:

  • You are not saving the random.choice return value anywhere. You are effectively checking whether [True, False] == True (and == False).

Other possible issues:

  • You haven't specified which interpreter of Python you are using. print is a function in Python 2, but not in Python 2. I know both assert(whatever) and assert whatever work in at least some versions of Python 2, but I'm not sure about print().
  • Semicolons are not used to separate statements in Python, newline is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.