2015-03-19 4 views
0

Я пытаюсь создать проект, в котором вы делаете выбор, чтобы победить дракона. Вот что у меня есть:После ответа на raw_input ничего не происходит

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() 

Проблема заключается в том, когда я отвечаю «1» или «2» код просто останавливается и не идет к первому == 1 или первого == 2

Пожалуйста, скажите мне, почему, я очень новичок в Python.

ответ

1

Ваша проблема в том, что raw_input() принимает ввод как строку. Попробуйте лить int().

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

Таким образом, вот отредактированный код:

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() 
+0

Ого, спасибо! –

+0

@NewCoder не проблема, если бы этот ответ помог вам, не могли бы вы принять его? (Нажмите зеленую галочку рядом с моим ответом, она дает нам репутацию :)) –

1

raw_input возвращает строки, а не целые числа:

>>> 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 

Следовательно, заменить:

if first == 1: 

с:

if first == '1': 

или:

if int(first) == 1: 
+0

Вы могли бы это сделать, но чтобы сохранить ее питоновой и простой, обычно используется литье 'int()' :) –

Смежные вопросы