python编程从入门到实践第二章

本文详细介绍了在VSCode中运行Python代码的常见问题及解决方案,包括如何避免“无法初始化设备PRN”错误,探讨了Python中名称错误的两种常见情况,并提供了字符串操作、拼接及空白处理的实用技巧。此外,还深入讲解了如何使用str()函数避免类型错误,以及Python之禅的内容。
  1. vs code 中直接输入代码运行,会报错:无法初始化设备 PRN。这时的解决办法是:先在命令行输入python,回车后运行代码就可以了。
  2. 名称错误的两种情况:1使用变量前忘记了给它赋值。 2输入变量名时拼写不正确。

使用方法修改字符串的大小写:

变量名.title() /全部首字母改为大写
变量名.upper() /全部字母改为大写
变量名.lower() /全部字母改为小写

使用+加号进行变量的拼接

添加空白的命令

使用\t添加制表符,在输出的语句时相贴着上一行输出的字符最后一位输出。如下图所示:
在这里插入图片描述
使用\n添加换行符,让字符串自动换到下一行。
而\n\t可以同时使用,效果也是两者的叠加。

关于删除字符串的方法:

str.rstrip([chars]) \删除字符串右边指定的字符,为空时即为删除空白符
str.lstrip([chars]) \删除字符串左边指定的字符,为空时同上
str.strip([chars]) \删除字符串两边指定的字符,为空同上。

使用函数str()避免类型错误

age=23
print("Happy "+str(age)+"rd Birthday")

这里的str()就是将这个整数用作字符串

python之禅

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
以下是《Python编程:从入门实践(第二版)》部分章节的习题答案: ### 第二章答案 - **练习2-1**: ```python message = "Hello World" print(message) ``` - **练习2-2**: ```python message = "Hello World" print(message) message = "This is a simple message" print(message) ``` - **练习2-3**: ```python name="scalpel" print(f"Hello {name}, would you like to learn some Python today?") ``` - **练习2-4**: ```python name = "Li hua" print(name.lower()) print(name.upper()) print(name.title()) ``` - **练习2-5**: ```python print('Albert Einstein once said,"A person who never made a mistake never tried anything new."') ``` - **练习2-6**: ```python famous_person = "Albert Einstein" message = f'{famous_person} once said,"A person who never made a mistake never tried anything new."' print(message) ``` - **练习2-7**: ```python name = "\tAlbert Einstein\t\n" print(name.lstrip()) print(name.rstrip()) print(name.strip()) ``` - **练习2-8**: ```python print(5+3) print(10-2) print(2*4) print(16/2) ``` - **练习2-9**: ```python num = 17 print(f"my favorite number is {num}") ``` ### 第七章答案 - **练习7 - 5**: ```python while True: age = input("Your age: ") if age == "quit": break age = int(age) if age < 3: print("You are free.ヽ(✿゚▽゚)ノ") elif age < 13: print("Your ticket is 13.") else: print("Your ticket is 15") ``` ### 第九章答案 - **练习9 - 15**: ```python import random lottery = [10,35,44,55,64,78,89,92,'r','b','c','d','e'] print(lottery) my_ticket = [] win_num = random.sample(lottery,5) print(f'中奖号码:{win_num}') i = 1 while True: '''choice返回的是一个元素,choices返回的是一个列表, 且可能列表中的值会重复,sample返回的是一个列表, 单列表中的值不会重复''' my_ticket = random.sample(lottery,k=5) if my_ticket != win_num: i += 1 continue else: # 报告执行循环多少次才中了大奖 print(f'第{i}次能够中奖!') break ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值