【Python】 Python 语言设计基础 - 第二章

Before we comb through the codes

《Python 语言程序设计基础》 高等教育出版社 - 第二版
Python Version 3.9.0 + Visual Studio 2019 / Visual Studio Code 1.52.1


Preparation

【Python】 下载 Python(Visual Studio 2019)的步骤
【Python】 Python 语言设计基础 - 第一章

示例代码 1.1

TempStr = input("Please input the temperature with the suffix C or F: ")
if TempStr[-1] in ['F', 'f']:
    C = (eval(TempStr[0: -1]) - 32) / 1.8
    print("It is equal to {:.2f}C".format(C))
elif TempStr[-1] in ['C', 'c']:
    F = 1.8 * eval(TempStr[0: -1]) + 32
    print("It is equal to {:.2f}F".format(F))
else:
    print("Grammatical error!")

Output:
S1.1

示例代码 1.2

给一种和书上不完全一样的方法

TempStr = 'Y'
while True:
    TempStr = input("Please input the temperature with the suffix C or F. (Enter N to exit)\nTemperature: ")
    if TempStr[-1] in ['N', 'n']:
        break
    if TempStr[-1] in ['F', 'f']:
        C = (eval(TempStr[0: -1]) - 32) / 1.8
        print("It is equal to {:.2f}C".format(C))
    elif TempStr[-1] in ['C', 'c']:
        F = 1.8 * eval(TempStr[0: -1]) + 32
        print("It is equal to {:.2f}F".format(F))
    else:
        print("Grammatical error!")

Output:
S1.2

示例代码 1.3

Python 没有变量类型,函数居然也没有参数类型!太神奇了😮

def tempConvert(ValueStr):
    if ValueStr[-1] in ['F', 'f']:
        C = (eval(ValueStr[0: -1]) - 32) / 1.8
        print("It is equal to {:.2f}C".format(C))
    elif ValueStr[-1] in ['C', 'c']:
        F = 1.8 * eval(ValueStr[0: -1]) + 32
        print("It is equal to {:.2f}F".format(F))
    else:
        print("Grammatical error!")

TempStr = input("Please input the temperature with the suffix C or F: ")
tempConvert(TempStr)

Output:
S1.3

思考与练习 2.6

def echo(str):
    print(str)

echo(input("Please input a line: "))

K2.6


ALL RIGHTS RESERVED © 2021 Teddy van Jerry
欢迎转载,转载请注明出处。


See also

Teddy van Jerry 的导航页

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值