菜鸡学python(二)

2021.8.26打卡

打卡代码

demo8:变量赋值中与C++不同之处


# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/26 8:59


# ——————解包赋值————————
a, b, c = 1, 2, 3
print(a, b, c)

# ——————Python交换两个变量的值——————

a, b = b, a
print(a, b)
# ——————比较运算符——————
# is, is not 比较的是比较对象的ID地址
list1 = [11, 22, 33]
list2 = [11, 22, 33]
print(list1 == list2)
print(list1 is list2)
print(a is not b)
print(list1 is not list2)

demo9:布尔运算符

# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/26 9:14

#布尔运算符 and 、or 、not 、in 、not in

# and==&& or==|| not==!

s='hello world'
print('w' in s)
print ('k' not in s)

#——————位运算符——————
print(4&8)
print(4|8)
print(4>>1)
print(4<<1)

demo10:布尔函数

# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/26 9:44
#测试布尔值
print(bool(False))
print(bool(0))
print(bool(None))
print(bool(""))
print(bool('hell world'))

demo11:if判断语句


# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/26 14:59
num = int(input('请输入一个整数'))
if num % 2 == 0:
    print("该数字为偶数")
else:
    print("该数字为奇数")
'''
90-100 A
80-89  B
70-79  C
60-69  D
60以下  E
'''
num2 = int(input('请输入一个整数'))
if 90 <= num2 <= 100:
    print("A")
elif 80 <= num2 <= 89:
    print("B")
elif 70 <= num2 <= 79:
    print("C")
elif 69 <= num2 <= 60:
    print("D")
elif num2 < 60:
    print("E")
else:
    print("成绩有误,数字不在有效范围")
# ——————if嵌套结构——————
answer = input("您是会员吗?y/n")
if answer == 'y':
    money = float(input('尊敬的会员,请输入那您付款金额 :'))
    money *= 0.8
    print("您需要实付金额为:", money)
else:
    answer2 = input('充值会员享受八折优惠,是否充值')
    if answer2 == 'y':
        money = float(input('尊敬的会员,请输入那您付款金额 :'))
        money *= 0.8
        print("您需要实付金额为:", money)
    else:
        money = float(input("垃圾平民,请输入付款金额"))
        print("您需要实付金额为:", money)
# ——————————if判断简写
a = int(input("输入第一个整数"))
b = int(input("输入第二个整数"))
print("第一个是否大于第二个", "yes" if (a >= b) else "No")

demo12:range函数和pass语句


# 博主链接:https://blog.csdn.net/qq_45148277
# email:daozhang123@foxmail.com
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2021/8/26 16:16
# pass语句 不起任何实际作用,仅仅占位让程序不报错
# range()三种创建方式
# 1
r=range(10)
print(r)
print(list(r))
#2
r=range(1,10)
print(list(r))
#3
r=range(1,10,2)
print(list(r))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是一个经典的问题,也称为“狼、羊、菜和农夫过河问题”。在这个问题中,农夫需要带着一只狼、一只羊和一些菜过河,但是船只只能容纳农夫和另外一只物品。如果农夫不在场,狼会吃羊,羊会吃菜。因此,农夫需要找到一种方法,使得所有物品都能安全地过河。 以下是一个简单的Python实现,其中使用了深度优先搜索算法来解决问题: ``` # 定义初始状态和目标状态 start_state = ['left', 'left', 'left', 'left'] goal_state = ['right', 'right', 'right', 'right'] # 定义有效状态 def is_valid(state): if state[0] != state[1] and state[1] != state[2]: return True return False # 定义搜索函数 def dfs(state, path): if state == goal_state: return path for i in range(8): new_state = state.copy() if i == 0: new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 1: if new_state[3] == new_state[0]: new_state[1] = 'right' if new_state[1] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 2: if new_state[3] == new_state[1]: new_state[0] = 'right' if new_state[0] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 3: if new_state[3] == new_state[2]: new_state[2] = 'right' if new_state[2] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 4: if new_state[3] == 'right': new_state[3] = 'left' elif i == 5: if new_state[3] == new_state[0]: new_state[1] = 'right' if new_state[1] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 6: if new_state[3] == new_state[1]: new_state[0] = 'right' if new_state[0] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' elif i == 7: if new_state[3] == new_state[2]: new_state[2] = 'right' if new_state[2] == 'left' else 'left' new_state[3] = 'right' if new_state[3] == 'left' else 'left' if is_valid(new_state) and new_state not in path: new_path = dfs(new_state, path + [new_state]) if new_path: return new_path return None # 执行搜索并输出结果 result = dfs(start_state, [start_state]) if result: for state in result: print(state) else: print('No solution found.') ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值