Skip to content

Latest commit

 

History

History
139 lines (111 loc) · 3.24 KB

131-1914620-值比较的应用_单选_判断.sy.md

File metadata and controls

139 lines (111 loc) · 3.24 KB
show version enable_checker
step
1.0
true

比较运算符

回忆

  • 这次研究了 is 和 ==
    • is 判断是否相等
    • 具体用 == 还是 is 和变量类型相关
  • 容器类对象的比较可以灵活运用 is 或者 ==
  • int、float、str 之类的相等判断最好用 == 和 !=
  • 具体怎么用呢??🤔

名画辨认

import random

# 定义画展中的画作列表
paintings = [
    {"name": "洛神赋图", "description": "描述洛神赋"},
    {"name": "清明上河图", "description": "宋朝市井景象"},
    {"name": "千里江山图", "description": "描绘庐山和鄱阳湖"},
    {"name": "富春山居图", "description": "描绘富春江两岸景色"},
    {"name": "汉宫春晓图", "description": "汉代宫女生活场景"},
]

# 指定要寻找的画作名称
target_painting = random.choice(paintings)["name"]

print("欢迎来到画展!")
print("这里有五幅画,其中一幅是你寻找的目标。")

# 开始游戏循环
while True:
    # 随机选择一幅画展示给玩家
    random_num = random.randint(0, len(paintings)-1)
    print(paintings[random_num]["description"])
    print("你要找的画是:")
    print(target_painting)
    print("是这幅吗?(yes/no)")

    # 获取玩家的输入
    choice = input().lower()
    if choice == "yes" and paintings[random_num]["name"] == target_painting:
        print("恭喜你找到了目标画作!游戏结束。")
        break
    elif choice == "no":
        print("很遗憾,你错过了目标画作。继续寻找吧。")
    else:
        print("无效的输入,请回答'yes'或'no'。")
  • 这很像判断题啊
    • 很好用
  • 可以做单选题吗?

单选题

import random

d ={
    "one": "only",
    "two": "twice",
    "three": "triple",
    "four": "fourth"}

l_keys = list(d)
num = random.randint(0,3)
word = l_keys[num]
print("需要解释的单词是:",word,"对应第几号?")
l_values = list(d.values())
random.shuffle(l_values)
answer = int(input(
))
print(l_values,answer,l_values[answer],word,sep="=")
while l_values[answer] != d[word]:
    print("Guess again!")
    answer = int(input())
print("You are right")
  • 这样就可以有单选了

图片描述

  • 可以扩大词库的大小吗?

扩展词库

import random

d ={
    "one": "only",
    "two": "twice",
    "three": "triple",
    "four": "fourth",
    "five": "fifth"}

l_keys = list(d)
num = random.randint(0,len(d)-1)
word = l_keys[num]
l_keys.remove(word)
print(l_keys)
random.shuffle(l_keys)
l_keys = l_keys[:3]
l_keys.append(word)
random.shuffle(l_keys)
print("需要解释的单词是:",word,"对应第几号?")
for num,key in enumerate(l_keys):
    print(num,d[key],sep=". ")
answer = int(input())
while l_keys[answer] != word:
    print("Guess again!")
    answer = int(input())
print("You are right")
  • 效果

图片描述

总结

  • 这次根据值比较的原理做了一些题目
  • 同时发现了这么个问题

图片描述

  • 同样的代码
    • id时等时不等
    • 这怎么理解?🤔
  • 下次再说 👋