Python_5_Codecademy_5_Pig Latin

2016-04-17  本文已影响25人  张一闻

<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:

  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. 划重点:
上一篇下一篇

猜你喜欢

热点阅读