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.
- 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
- Conversion & output
word = word.lower()
pig_latin = word[1:len(word)] + word[0] + "ay"
print "Pig Latin converted: " + pig_latin
- 汇总 & 试验
# -*- 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
- 划重点:
- len()
- .lower()
- .isalpha(), .isdigit()