为什么运行模块和输入后我的功能不能运行?

时间:2015-08-20 10:44:46

标签: python-3.x

'''
Pig Latin is a program game, which
moves the first letter of the word you entered to the end and adds "ay."
So "Python" becomes "ythonpay."
'''

print ("Pig Latin") #welcoming message for the program.

word = input("Input a word in English ") #user input defined variable

def pigtranslator(): #function that does translation from english to pig language
if 1 > 0: #true
    wordlen = word.len #assigns the length of word to a variable
    translation == word[1:wordlen] + word[0] + 'ay' #translates the word
    print (trasnlation) #prints the translation

else:
    print("The word you've entered is not correct. Please enter a new word")

return

运行此模块后,我要求输入但是输入后没有任何事情发生,并且函数pigtranslator没有执行。

1 个答案:

答案 0 :(得分:0)

这是因为你没有调用你的函数。你需要在模块的末尾添加pigtranslator()

print ("Pig Latin") #welcoming message for the program.

word = input("Input a word in English ") #user input defined variable

def pigtranslator(): #function that does translation from english to pig language
  if 1 > 0: #true
    wordlen = word.len #assigns the length of word to a variable
    translation == word[1:wordlen] + word[0] + 'ay' #translates the word
    print (trasnlation) #prints the translation

  else:
    print("The word you've entered is not correct. Please enter a new word")

return

pigtranslator()

但是作为一种更优雅的方式,您可以移动以下部分:

print ("Pig Latin") #welcoming message for the program.
word = input("Input a word in English ") #user input defined variable

到您的函数并使用模块调用它:

print my_module.pigtranslator()