Python_5_Codecademy_5_Pig Latin

<a href="http://www.jianshu.com/p/54870e9541fc">总目录</a>


课程页面:https://www.codecademy.com/
内容包含课程笔记和自己的扩展折腾


Pig Latin is a language game, where you move the first letter of the word to the end and add "ay." So "Python" becomes "ythonpay." To write a Pig Latin translator in Python, here are the steps we'll need to take:

  • Ask the user to input a word in English.
  • Make sure the user entered a valid word.
  • Convert the word from English to Pig Latin.
  • Display the translation result.
  1. Input & check
print "Welcome to the world of Pig Latin."
word = ""
# make sure the user gives a valid word: 
# not empty, not including digits or other symbols
while len(word) == 0 or not word.isalpha():
      word = raw_input("Please give me a word in English: ")
print "You've entered a valid word."

测试:
Welcome to the world of Pig Latin.
Please give me a word in English: 20
Please give me a word in English: 1ed
Please give me a word in English: stupid
You've entered a valid word.
Process finished with exit code 0

  1. Conversion & output
word = word.lower()
pig_latin = word[1:len(word)] + word[0] + "ay"
print "Pig Latin converted: " + pig_latin
  1. 汇总 & 试验
# -*- coding: utf-8 -*-
print "Welcome to the world of Pig Latin."
word = ""
while len(word) == 0 or not word.isalpha():
      word = raw_input("Please give me a word in English: ")
print "You'v entered a valid word.
#
"word = word.lower()
pig_latin = word[1:len(word)] + word[0] + "ay"
print "Pig Latin converted: " + pig_latin

测试:
Welcome to the world of Pig Latin.
Please give me a word in English: 派桑
Please give me a word in English: 123
Please give me a word in English: $$$
Please give me a word in English: XDD
You'v entered a valid word.
Pig Latin converted: ddxay
Process finished with exit code 0

  1. 划重点:
  • len()
  • .lower()
  • .isalpha(), .isdigit()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容