2015-03-19 15 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

哇哦,謝謝! – 2015-03-19 03:01:15

+0

@NewCoder沒問題,如果這個答案對你有幫助,你會介意接受它嗎? (點擊我的答案旁邊的綠色複選標記,它給了我們兩個人的聲望:)) – 2015-03-19 03:02:03

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

你可以這樣做,但爲了保持pythonic和簡單,鑄造'int()'通常是首選:) – 2015-03-19 03:03:51