2013-07-08 20 views
1

我想通過codeacademy學習python,我正在瀏覽過去的課程,但我無法弄清楚我做錯了什麼。我想我正確地複製了一切。請幫助我與我的其他如果在python循環?

該任務是檢查用戶輸入單詞,看它是否至少包含一個字符。如果它包含多個字符,程序應該打印出用戶在開頭輸入的單詞。如果沒有,程序應該說「空」。

該代碼讓我輸入一個單詞,但即使該單詞有多個字符,也不會打印出單詞。我覺得這個解決方案可能很簡單,但我無法弄清楚。我認爲分號是在正確的空間。我非常感謝你的幫助

print "Welcome to the English to Pig Latin translator!" 
original = raw_input("tell me your secrets") 
def true_function(): 
    if len(original)>= 1: 
     print(original) 
    else: 
     print("empty") 
+0

你應該使用'輸入()'在Python 3 – squiguy

+0

@squiguy這不是蟒蛇3(Codecademy網站教2.7) – TerryA

+0

@Haidro我看到'打印()'。我想我應該讀更多:)。 – squiguy

回答

1

你需要調用true_function()爲它將被執行

做這樣的事情

print "Welcome to the English to Pig Latin translator!" 

def true_function(): 
    original = raw_input("tell me your secrets") 
    if len(original)>= 1: 
     print(original) 
    else: 
     print("empty") 
true_function() 

通知我怎麼稱呼true_function()你只是把輸入之前和多數民衆贊成,但知道輸入被要求在函數然後通過條件跑

這裏是在功能的一些教程,如果你不完全瞭解

Tutorials point: Functions

ZetCode calling functions

+0

哦哇,解決方案非常簡單,現在它的工作原理:D 我首先看到了你的,所以我會在時間倒數時接受它。 非常感謝! – user1476390

+0

沒問題當我學會繼續它時,我使用了代碼學院:) – Serial

5

這是因爲你永遠不會撥打true_function()函數。

您可以刪除,而只是有:

print "Welcome to the English to Pig Latin translator!" 
original = raw_input("tell me your secrets") 

if len(original)>= 1: 
    print(original) 
else: 
    print("empty") 

或者撥打true_function()之後,傳遞變量original作爲參數:

def true_function(original): 
    if len(original)>= 1: 
    print(original) 
    else: 
    print("empty") 

print "Welcome to the English to Pig Latin translator!" 
original = raw_input("tell me your secrets") 
true_function(original) 
0
original = raw_input(...) 

將用戶輸入返回到稱爲輸出的變量。

def true_function(): 

這個樣子定義了一個函數。注意它並沒有真正做任何事情,更像是告訴python像true_function()這樣的東西存在。

現在如果您撥打true_function()您的代碼將起作用。但我會建議以下更改。

def true_function(arg): 
    if len(arg)>= 1: 
     print(arg) 
    else: 
     print("empty") 

現在注意,你的函數接受了一個名爲arg的參數。當你調用這個函數,你給它在函數調用中的變量像

true_function(original) 

看到python function documentation更多的細節,你需要調用函數

0

伴侶。此外,該函數應該採用字符串參數,因爲我假設您將字符串作爲輸入。

def true_function(string): 
    if len(original)>= 1: 
     print(original) 
    else: 
     print("empty") 

print "Welcome to the English to Pig Latin translator!" 
original = raw_input("tell me your secrets") 
true_function(original)