Python 入门教程 6 ---- PygLatin

1 练习:用python来输出这句话"Welcome to the English to Pig Latin translator!"

[python] 

print "Welcome to the English to Pig Latin translator!"  

 

41 介绍了Python的输入,Python里面我们可以通过raw_input来实现出入

     2 比如我们使用name = raw_ijnput("what's your name") , 那么这里将会在what's your name提示我们输入,并把结果保存到name里面

     3 练习:使用original变量来接受任何的输入

[python]  

print "Welcome to the English to Pig Latin translator!"  

original = raw_input("welcome to the Python:")  

 

42 介绍了我们在输入的时候经常出现输入空字符串的情况,因此我们需要去检查是否是空字符串

    2 练习:在上一节的输入的值进行判断,如果不是空字符串那么打印这个值,否则直接输出"empty"

[python]  

print "Welcome to the English to Pig Latin translator!"  

original = raw_input("welcome to the Python:")  

if len(original) > 0:  

    print original  

else:  

    print "empty"  

 

43 介绍了怎样判断一个字符串是数字的方法,我们可以通过isalpha()来判断

如果是阿拉伯数字,则isalpha的值为false ,否则为TRUE

   2 比如有一个变量为x = "123",那么x.isalpha()是True

   3 练习:通过变量original的输入值来判断是否是一个数字串

[python]  

print "Welcome to the English to Pig Latin translator!"  

original = raw_input("welcome to the Python:")  

  

if original.isalpha():  

    print "True"  

else:  

    print "False"  

 

 第五节

    1 练习:利用多次的输入来判断是否是数字串和非空字符串

[python] 

print "Welcome to the English to Pig Latin translator!"  

original = raw_input("welcome to the Python:")  

if original.isalpha():  

    print "True"  

else:  

    print "False"  

      

original = raw_input("welcome to the Python:")  

if len(y) == 0:  

    print "empty"  

else:  

    print "no empty"  

 

 第六节

    1 回顾了一下之前的String的lower()函数

    2 练习

       1 设置变量word等于original.lower()

       2 设置变量first等于word的第一个字符

[python]  

pyg = 'ay'  

  

original = raw_input('Enter a word:')  

word = original.lower()  

first = word[0]  

if len(original) > 0 and original.isalpha():  

    print original  

else:  

    print 'empty'  

 

 第六节

    1 介绍了if语句里面还可以嵌套语句

    2 练习:判断上一节里面的first字符是否是元音字符是的话输出"vowel",否则输出"consonant"

[python]  

pyg = 'ay'  

  

original = raw_input('Enter a word:')  

word = original.lower()  

first = word[0]  

if len(original) > 0 and original.isalpha():  

    if first == 'a' or first == 'i' or first == 'o' or first == 'u' or first == 'e':  

        print "vowel"  

    else:  

        print "consonant"  

else:  

    print 'empty'  

 

 第七节

     1 利用String的+操作,产生一个新的变量new_word等于word+pyg

     2 练习:把print "vowel"替换成print new_word

[python]  

pyg = 'ay'  

original = raw_input('Enter a word:')  

word = original.lower()  

first = word[0]  

new_word = word+pyg  

  

if len(original) > 0 and original.isalpha():  

    if first == 'a' or first == 'i' or first == 'o' or first == 'u' or first == 'e':  

        print new_word  

    else:  

        print "consonant"  

else:  

    print 'empty'  

 

44 介绍了String中得到子串的方法,比如我们有一个字符串s = "foo",现在s[0:2]就是"fo"

45 如果结束是末尾,那么可以直接写成这样s[i:],省略第二个数

    2 练习:在嵌套的if语句里面设置new_word的值为word从第1位到最后一位+变量pyg

[python]  

pyg = 'ay'  

original = raw_input('Enter a word:')  

word = original.lower()  

first = word[0]  

new_word = word+pyg  

  

if len(original) > 0 and original.isalpha():  

    if first == 'a' or first == 'i' or first == 'o' or first == 'u' or first == 'e':  

        new_word = word[1:]+pyg  

        print new_word  

    else:  

        print "consonant"  

else:  

print 'empty'  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值