Python 入门教程 6 ---- PygLatin


 
 第一节
     1 练习:使用Python来输出这句话"Welcome to the English to Pig Latin translator!"
[python] 
print "Welcome to the English to Pig Latin translator!"  
 
 第二节
     1 介绍了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:")  
 
 第三节
    1 介绍了我们在输入的时候经常出现输入空字符串的情况,因此我们需要去检查是否是空字符串
    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"  
 
 第四节
   1 介绍了怎样判断一个字符串是数字的方法,我们可以通过isalpha()来判断
   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'  
 
 第八节
    1 介绍了String中得到子串的方法,比如我们有一个字符串s = "foo",现在s[0:2]就是"fo"
    2 如果结束是末尾,那么可以直接写成这样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'  

相关内容

    暂无相关文章

评论关闭