Kellen Cole
Kellen Cole

Reputation: 27

After answering to a raw_input, nothing happens

I am trying to make a project where you make choices to defeat a dragon. Here is what I have:

import time

print "Hello"
time.sleep(1.5)

print "Welcome to Kill the Dragon."
time.sleep(2)

print "In this game, you will choose an option, and if you make the right choices, you will slay the dragon."
time.sleep(5)

print "You are walking through a forest on a cold, damp, windy night."
time.sleep(3)

print "You see a castle, and as you are looking for a shelter, you decide to try your luck there."
time.sleep(5)

print "You are greeted by a knight in shining armor. He gives you a questioning look. What do you say?"
time.sleep(4)

first = raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food ")
time.sleep(5)
if first == 1:
    print "Ok, we could all use some help from this bad storm outside. Please let me lead you to a good place."
    time.sleep(5)
    print "After a good dinner, you ask if there is anything you can do to help."
    time.sleep(2)
if first == 2:
    print "Ugg, I hate filthy peasants! If you go kill the dragon for us, maybe we will let you stay."
    time.sleep(4)
    print "1: Insist on getting inside the castle"
    print "2: Ask the knight for armor, a weapon, and a steed"
    second2 = raw_input()   

The problem is when I answer "1" or "2," the code just stops running and it doesn't go to first == 1 or first == 2

Please tell me why, I am very new at Python.

Upvotes: 0

Views: 344

Answers (2)

John1024
John1024

Reputation: 113834

raw_input returns strings, not integers:

>>> first = raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food ")
1: Say you are looking for shelter from the storm 2: Say you are lost and need food 2
>>> first
'2'
>>> first == 2
False
>>> first == '2'
True

Consequently, replace:

if first == 1:

with:

if first == '1':

or with:

if int(first) == 1:

Upvotes: 1

A.J. Uppal
A.J. Uppal

Reputation: 19264

Your problem is that raw_input() takes input as a string. Try casting int() to it.

>>> x = raw_input("Enter: ")
Enter: 1
>>> x
"1"
>>> x = int(raw_input("Enter: "))
Enter: 1
>>> x
1

Thus, here is your edited code:

import time

print "Hello"
time.sleep(1.5)

print "Welcome to Kill the Dragon."
time.sleep(2)

print "In this game, you will choose an option, and if you make the right choices, you will slay the dragon."
time.sleep(5)

print "You are walking through a forest on a cold, damp, windy night."
time.sleep(3)

print "You see a castle, and as you are looking for a shelter, you decide to try your luck there."
time.sleep(5)

print "You are greeted by a knight in shining armor. He gives you a questioning look. What do you say?"
time.sleep(4)

first = int(raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food "))
time.sleep(5)
if first == 1:
    print "Ok, we could all use some help from this bad storm outside. Please let me lead you to a good place."
    time.sleep(5)
    print "After a good dinner, you ask if there is anything you can do to help."
    time.sleep(2)
if first == 2:
    print "Ugg, I hate filthy peasants! If you go kill the dragon for us, maybe we will let you stay."
    time.sleep(4)
    print "1: Insist on getting inside the castle"
    print "2: Ask the knight for armor, a weapon, and a steed"
    second2 = raw_input() 

Upvotes: 1

Related Questions