2017-05-26 51 views
1

你好,我正在尋找取代Python中的故事中的單詞的基礎知識。 到目前爲止我有下面的代碼。我正在運行Spyder。如何在python中創建可編輯的故事?

收集的名字從用戶

故事用戶

def Travsnguard(): 
    print ("Welcome to Travsngaurd!") 

    #relative = input("Enter a type of relative.") 
    name = input("Enter a name") 
    #verb = input("Enter an 'ing' verb") 
    #adjective = input("Enter an adjective") 
    #noun = input("Enter a noun") 
    print ("Hello,", name, "your journey starts now!") 
    enter code here 

我的程序只運行並沒有做任何事情。

+0

真的很愚蠢的問題,但您確實在代碼的末尾調用了'Travsnguard()',對吧? – user3080953

回答

0

這被稱爲字符串連接。在這種情況下,您可以使用'+'符號。

name = input("What is your name?") 
print("Hi, " + name + " nice to meet you." 

在更復雜的情況下,您可能希望執行字符串插值 - 這是採取一個固定的字符串和插入值,就像這樣:

name = input("What is your name?") 
birthsign = input("What is your birthsign?") 
race = input("What is your race?") 
father = input("Who was your father?") 

#%s = insert string - we then pass the variables to insert in order of use 
storyString = "Welcome %s son of %s of the sign %s and race %s. You have travelled far." % (name, father, birthsign, race) 

print(storyString) 
+0

哇,你們很快就回復謝謝你的幫助! – Limitlessmatrix

0

我切換到原始投入,以確保我們得到的字符串和然後在最後將結果連接成一個字符串。像這樣的事情你正在嘗試做什麼?

def Travsnguard(): 
    print ("Welcome to Travsngaurd!") 
    relative = input("Enter a type of relative: ") 
    name = input("Enter a name: ") 
    verb = input("Enter an 'ing' verb: ") 
    adjective = input("Enter an adjective: ") 
    noun = input("Enter a noun: ") 
    print ("Hello,", name, "your journey starts now!") 
    print ("You are here with your " + adjective + " " + relative + " raul. and you\ 
are going to go " + verb + ". You are going to use your " + noun + ".") 
Travsnguard() 
+0

'raw_input'沒有在python 3中定義(注意這個問題被標記爲python 3.6) – user3080953

+0

那真是令人尷尬,謝謝。 –

+0

這幫助了很多,但我認爲我可以在3.6上使用raw_input()函數,它只允許使用input()函數。我確實現在看起來好多了。 – Limitlessmatrix