python笨办法学习-practice31

本文介绍了如何在Python编程中使用if,else,andelif创建包含条件判断的脚本,通过模拟用户在暗室中选择门和应对不同情况,展示了如何根据用户输入做出决定。
摘要由CSDN通过智能技术生成

作出决定

这本书的上半部分你打印了一些东西,而且调用了函数,不过一切都是直线式进行的。你的脚本从最上面一行开始,一路运行到结束,但其中并没有决定程序流向的分支点。现在你已经学了 if, else, 和elif ,你就可以开始创建包含条件判断的脚本了。

上一个脚本中你写了一系列的简单提问测试。这节的脚本中,你将需要向用户提问,依据用户的答案来做出决定。把脚本写下来,多多鼓捣一阵子,看看它的工作原理是什么。

print("You enter a dark room with two doors. Do you go through door #1 or door #2?")
#你进入一个有两扇门的暗室。你是从1号门走还是从2号门走?

door = input(">") #用户输入1或2

#如果进入1号门
if door =="1":
    print("There's a giant bear here eating a cheese  cake. What do you do?")#这里有一只大熊在吃奶酪蛋糕。你要做什么?
    print("1. Take the cake.")#1-吃蛋糕
    print("2. Scream at the bear.")#2-对着熊尖叫

    bear = input(">")
    if bear =="1":
      print("The bear eats your face off. Good job!") #熊吃掉了你的脸。干得好
    elif bear == "2":
      print("The bear eats your legs off. Good job!") #熊吃掉了你的腿。干得好
    else:
      print("well, doing %s is probably better. Bear runs away." % bear)#“好吧,做%s可能更好。熊跑了

elif door == "2":
    print("You stare into the endless abyss at Cthulhu's retina.")#你凝视着Cthulhu视网膜无尽的深渊
    print("1. Blueberries.") #蓝莓
    print("2. Yellow jacket clothespins.")#黄色夹克衣夹
    print("3. Understanding revolvers yelling melodies.")#了解左轮手枪大喊的旋律

    insanity = input(">") #精神时常

    if insanity == "1" or insanity == "2":
        print("Your body survives powered by a mind of jello. Good job!") #你的身体靠果冻的力量生存。干得好
    else:
        print("The insanity rots your eyes into a pool of muck. Good job!") #这种疯狂会把你的眼睛腐蚀成一滩淤泥。干得好
else:
    print("you stumble around and fall on a knife and die. Good job!")#你踉踉跄跄地跌倒在刀上就死了。干得好
#Pycharm运行结果:
#第一次运行:
You enter a dark room with two doors. Do you go through door #1 or door #2?
>1
There's a giant bear here eating a cheese  cake. What do you do?
1. Take the cake.
2. Scream at the bear.
>2
The bear eats your legs off. Good job!
#第二次运行:
You enter a dark room with two doors. Do you go through door #1 or door #2?
>1
There's a giant bear here eating a cheese  cake. What do you do?
1. Take the cake.
2. Scream at the bear.
>1
The bear eats your face off. Good job!
#第3次运行:
You enter a dark room with two doors. Do you go through door #1 or door #2?
>2
You stare into the endless abyss at Cthulhu's retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
>1
Your body survives powered by a mind of jello. Good job!
#第4次运行:
You enter a dark room with two doors. Do you go through door #1 or door #2?
>2
You stare into the endless abyss at Cthulhu's retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
>3
The insanity rots your eyes into a pool of muck. Good job!
#第5次运行:
You enter a dark room with two doors. Do you go through door #1 or door #2?
>stuff
you stumble around and fall on a knife and die. Good job!
#第6次运行:
You enter a dark room with two doors. Do you go through door #1 or door #2?
>1
There's a giant bear here eating a cheese  cake. What do you do?
1. Take the cake.
2. Scream at the bear.
>apple
well, doing apple is probably better. Bear runs away.

常见问题回答

Q1:可以用多个 if/else 来取代 elif 吗?

A1:有时候可以,不过这也取决于额 if/else 是怎样写的,而且这样一来 python 就需要去 检查每一处 if/else,而不是像 if/elif/else 一样,只要检查到第一个 True 就可以停下来了。试着写些代码看两者有何不同。

Q2:怎样判断一个数字处于某个值域中?

A2:两个办法:经典语法是使用 1 < x < 10,或者用 x in range(1, 10) 也可以。

Q3:怎样用 if/elif/else 区块实现四个以上的条件判断?

A3:简单,多写几个 elif 区块就可以了

缩进错误:

IndentationError: unindent does not match any outer indentation level-缩进错误

If

    空四格开始写

Elif

    空四格开始写

Else

    空四格开始写

同一个条件下 的if elif else必须在同一列。

根据用户反馈计算加减乘除的小例子(if/elif/else)

print("计算方式是加减乘除的哪一种?")
way = input(">")
if way == "加":
    number_a = int(input("请输入a:"))
    number_b = int(input("请输入b:"))
    print(number_a+number_b)

elif way == "减":
    number_c = int(input("请输入c:"))
    number_d = int(input("请输入d:"))
    print(number_c - number_d)

elif way == "乘":
    number_c1 = int(input("请输入c1:"))
    number_d1 = int(input("请输入d1:"))
    print(number_c1 * number_d1)

elif way == "除法":
    number_c2 = int(input("请输入c2:"))
    number_d2 = int(input("请输入d2:"))
    print(number_c2 / number_d2)

else:
    print("Number error")

#pycharm运行结果:

计算方式是加减乘除的哪一种?

>加

请输入a:100

请输入b:100

200

缺点是:每次运行结束后会暂停,需要重新运行。

有小伙伴知道该怎么连续多次的运行吗?

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值