习题4.1-4.5.md

#for循环
word = input("Enter a word:")
print("\nHere's each letter in your word:")
for letter in word:
    print(letter)
    
Enter a word:lzq

Here's each letter in your word:
l
z
q
#range函数
print("Counting:")
for i in range(10):
    print(i,end=" ")
    
print("\n\nCounting by five:")
for i in range(0,50,5):
    print(i,end=" ")
    
print("\n\nCounting backwards:")
for i in range(10,0,-1):
    print(i,end=" ")
    
Counting:
0 1 2 3 4 5 6 7 8 9 

Counting by five:
0 5 10 15 20 25 30 35 40 45 

Counting backwards:
10 9 8 7 6 5 4 3 2 1 
# len()函数和in运算符
message = input("Enter a message:")

print("\nThe length of your message is:",len(message))

print("\nThe most common letter in the English language'e'")
if "e" in message:
    print("is in your message.")
else:
    print("is not in your message.")
    
Enter a message:lzq like to study.

The length of your message is: 18

The most common letter in the English language'e'
is in your message.
#字符串的索引
import random

word = "index"
print("The word is:%s\n"%word)

high = len(word)
low = -len(word)

for i in range(10):
    position = random.randrange(low,high)  #不包含high
    print("word[%d]:%s"%(position,word[position]))
    
The word is:index

word[2]:d
word[-4]:n
word[3]:e
word[-1]:x
word[3]:e
word[1]:n
word[-1]:x
word[-2]:e
word[-5]:i
word[3]:e

猜你喜欢

转载自blog.csdn.net/DMU_lzq1996/article/details/82945534
4.5