美文网首页
Python_5_Codecademy_5_Pig Latin

Python_5_Codecademy_5_Pig Latin

作者: 张一闻 | 来源:发表于2016-04-17 19:31 被阅读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:

  • 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()

相关文章

  • Python_5_Codecademy_5_Pig Latin

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

  • Latin Music and Salsa

    Latin Music and Salsa Latin music is very common in count...

  • 拉丁舞伦巴有哪些学习心得呢?

    【latin】

  • igpay atinlay

    标题是“pig latin”,用 pig latin 的方式说的话,就是“igpay atinlay”。 每次复习...

  • Pig Latin

    把指定的字符串翻译成 pig latin。 Pig Latin 把一个英文单词的第一个辅音或辅音丛(consona...

  • Everyday Latin

    Abiit ad maiores. (S/he has gone to the ancestors.) Abiit...

  • Latin 2

    拉丁美洲是一个多民族的组合,因此拉丁音乐是以多种音乐的融合而形成的一种多元化的混合型音乐。无论是欧洲的白人音乐、非...

  • Leetcode PHP题解--D60 824. Goat La

    D60 824. Goat Latin 题目链接 824. Goat Latin 题目分析 给定一个句子,由大小写...

  • 拉美| 性别平等

    Gender Inequality in Latin America wage discriminationaga...

  • 【2019-05-29】关于pig

    pig包括两部分:(1)用于描述数据流的语言,Pig Latin(2)用户执行Pig Latin程序的执行环境。执...

网友评论

      本文标题:Python_5_Codecademy_5_Pig Latin

      本文链接:https://www.haomeiwen.com/subject/uyiolttx.html