Python 小练习 Pig Latin
2016-06-16 16:59
344 查看
要求将输入的单词,第一个单词移到,末尾,并添加ay
比如输入 number,输出结果为umbernay
当然,还要判断输入是否为有效单词
以下是自己的练习答题
def pig():
key=input('please enter the work:\n')
if type(key) is str:
key=(key[1:]+key[0]+'ay')
print(key)
else:
print('wrong input')
pig()
然后看看codecademy上面教程里的方法,另一种思路,也不错。
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha(): # 通过len()函数确认字符串大于0 , 通过 isalpha()函数来确认输入是不是字母字符,保证不包含数字等信息。
word=original.lower() # 将输入单词小写化,其实有没有无所谓
first=word[0]
new_word=word+first+pyg
new_word=new_word[1:len(new_word)]
print new_word
else:
print 'empty'
比如输入 number,输出结果为umbernay
当然,还要判断输入是否为有效单词
以下是自己的练习答题
def pig():
key=input('please enter the work:\n')
if type(key) is str:
key=(key[1:]+key[0]+'ay')
print(key)
else:
print('wrong input')
pig()
然后看看codecademy上面教程里的方法,另一种思路,也不错。
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha(): # 通过len()函数确认字符串大于0 , 通过 isalpha()函数来确认输入是不是字母字符,保证不包含数字等信息。
word=original.lower() # 将输入单词小写化,其实有没有无所谓
first=word[0]
new_word=word+first+pyg
new_word=new_word[1:len(new_word)]
print new_word
else:
print 'empty'
相关文章推荐
- Python中的多行注释文档编写风格汇总
- 在macOS上通过pyenv安装和切换多版本Python
- Python安装及环境变量配置
- 制作大漠字库并用python调用大漠工具方法来识别文字
- python 遍历文件夹 文件
- Python之正则表达式
- 【Python自动化运维之路Day6】
- Python构造自定义方法来美化字典结构输出的示例
- python BIF /操作符 / If else / assert
- python 找出序列中出现次数最多的元素方法。
- [leetcode]47. Permutations II,python实现
- 浅谈Python中chr、unichr、ord字符函数之间的对比
- Python下载
- python中的urlencode与urldecode
- Python3.4-字典笔记
- 详解Python中 __get__和__getattr__和__getattribute__的区别
- Windows系统下为 Python安装 Pcapy模块的方法
- python语言学习笔记之二
- Untitled
- mxnet的python包导入的前前后后