习题4.6-4.11.md

# 创建新的字符串
message = input("Enter a message:")
new_message = ""
VOWELS = "aeiou"

print()
for letter in message:
    if letter.lower() not in VOWELS:
        new_message += letter
        print("A new string has been created:",new_message)

print("\nYour new message without vowels is:",new_message)
Enter a message:aloiuhf

A new string has been created: l
A new string has been created: lh
A new string has been created: lhf

Your new message without vowels is: lhf
#字符串切片
word = "pizza"

print("""
      Slicing'Cheat Sheet'
""")

print("Enter the begining and ending index for your slice of'pizza'")
print("Press the enter key at 'Begin'to exit.")

start = None
while start != "":
    start = (input("\nStart:"))
    
    if start:
        start = int(start)
        
        finish = int(input("Finish:"))
        
        print("word[%d:%d] is:%s"%(start,finish,word[start:finish]))
        

      Slicing'Cheat Sheet'

Enter the begining and ending index for your slice of'pizza'
Press the enter key at 'Begin'to exit.

Start:2
Finish:3
word[2:3] is:z

Start:
#元组的创建
inventory = ()

if not inventory:
    print("You are empty-handed.")

input("\nPress the enter key to continue.")

inventory = ("sword",
             "armor",
             "shield",
             "heading potion")

print("\nThe tuple inventory is:",inventory)

print("\nYour items:")
for item in inventory:
    print(item)
You are empty-handed.

Press the enter key to continue.

The tuple inventory is: ('sword', 'armor', 'shield', 'heading potion')

Your items:
sword
armor
shield
heading potion
#元组创建
inventory = ("sword","armor","shield","heading potion")
print("Your item:")
for item in inventory:
    print(item)
#获取元组长度
print("You have %d items in your possession."%len(inventory))
#in测试成员关系
if "heading potion" in inventory:
    print("You will live to fight another day.")
#元组索引
index = int(input("\nEnter the index number for an item in inventory."))
print("At %d is %s"%(index,inventory[index]))
#元组切片
start = int(input("\nEnter the index number of begining:"))
finish = int(input("\nEnter the index number of finishing:"))
print("inventory[%d,%d] is %s"%(start,finish,inventory[start:finish]))
#元组链接
chest = ("gold","game")
print("\nYou find a chest.It contains:",chest)
print("You add the contents of the chest to your inventory:")
inventory += chest
print("Your inventory is now:",inventory)

Your item:
sword
armor
shield
heading potion
You have 4 items in your possession.
You will live to fight another day.

Enter the index number for an item in inventory.1
At 1 is armor

Enter the index number of begining:1

Enter the index number of finishing:3
inventory[1,3] is ('armor', 'shield')

You find a chest.It contains: ('gold', 'game')
You add the contents of the chest to your inventory:
Your inventory is now: ('sword', 'armor', 'shield', 'heading potion', 'gold', 'game')
#猜文字游戏
import random
WORDS = ("python","jumple","easy","different","answer","xylophone")

word = random.choice(WORDS)

correct = word

jumble = ""

while word:
    position = random.randrange(len(word))
    jumble += word[position]
    
    word = word[:position] + word[(position + 1):]

print("Welcome to word jumble!")
print("The jumble is:",jumble)

guess = input("\nYour guess:")
while guess != correct and guess != "":
    print("Sorry,that's not it.")
    guess = input("Your guess:")
    
if guess == correct:
    print("That's it!You guessed it!")
Welcome to word jumble!
The jumble is: exponlhyo

Your guess:py
Sorry,that's not it.
Your guess:python
Sorry,that's not it.
Your guess:python
Sorry,that's not it.
Your guess:easy
Sorry,that's not it.
Your guess:jumple
Sorry,that's not it.
Your guess:answer
Sorry,that's not it.
Your guess:xylophone
That's it!You guessed it!

猜你喜欢

转载自blog.csdn.net/DMU_lzq1996/article/details/82950020
4.6