“笨办法”学Python 3 ——练习26 恭喜你,来做个测试吧!

练习题目

在这个练习中,通过修复一个烂程序员的代码来练习和他们打交道。已将几个练习的代码复制到了一个文件里,然后随机删除一些字符并加入一些错误。这些错误大多数 Python 都会告诉你,不过一些可能是计算错误,或者是一些字符格式或拼写错误,需要你自己发现。
在这个练习中,你的工作就是纠正这个文件。这个练习的重点不在于输入,而在于修复好一个现有的文件。你需要去官网下载这个:
https://learnpythonthehardway.org/python3/exercise26.txt
其实就是练习 11、15、24和29的代码的代码打乱。

练习26 修改后代码

from sys import argv
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input() #缺少变量height
print("How much do you weigh?", end=' ')  #缺少右边括号
weight = input()

print(f"So, you're {age} old, {height} tall and {weight} heavy.")

script, filename = argv #未调用argv模块

txt = open(filename) #filename缺少a

print(f"Here's your file {filename}:") #缺少转换字符串格式的f
print(txt.read()) #少写t

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read()) #多出了()两个括号,read()函数用点连接

print("Let's practice everything.")  #多个单引号,修改为双引号
print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.') #print()不能换行

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""

print("--------------") #缺少右边的双引号
print(poem)
print("--------------") #缺少左边的双引号


five = 10 - 2 + 3 - 6 #减号后面少数字6
print(f"This should be five: {five}") #缺少右边的括号

def secret_formula(started):  #缺少冒号
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100  #缺少运算符号
    return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point) #缺少变量crates

# remember that this is another way to format a string
print("With a starting point of: {}".format(start_point))
# it's just like with an f"" string
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")

start_point = start_point / 10

print("We can also do that this way:")
formula = secret_formula(start_point) #参数名缺少下划线_
# this is an easy way to apply a list to a format string
print("We'd have {} beans, {} jars, and {} crates.".format(*formula))



people = 20
cats = 30 #cates变量与后面的cats变量不符
dogs = 15


if people < cats:
    print("Too many cats! The world is doomed!") #缺少两边括号

if people > cats:
    print("Not many cats! The world is saved!") #对比符号错误,不应该与上面的符号相同

if people < dogs:
    print("The world is drooled on!")

if people > dogs:  #缺少冒号
    print("The world is dry!")


dogs += 5

if people >= dogs:
    print("People are greater than or equal to dogs.")

if people <= dogs: #缺少冒号
    print("People are less than or equal to dogs.") #缺少右边的双引号


if people == dogs: #等于对比,用==。
    print("People are dogs.")

输出结果

#在终端输入以下代码
python C:\Users\***\Desktop\Python3_exercises\ex26.py C:\Users\***\Desktop\Python3_exercises\ex15_sample.txt 
How old are you? 38
How tall are you? 6'2"
How much do you weigh? 180lbs
So, you're 38 old, 6'2" tall and 180lbs heavy.
Here's your file C:\Users\limin\Desktop\Python3_exercises\ex15_sample.txt:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots offun to have in here.
Type the filename again:
> C:\Users\***\Desktop\Python3_exercises\ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots offun to have in here.
Let's practice everything.
You'd need to know 'bout escapes with \ that do
 newlines and    tabs.
--------------

        The lovely world
with logic so firmly planted
cannot discern
 the needs of love
nor comprehend passion from intuition
and requires an explanation

                where there is none.

--------------
This should be five: 5
With a starting point of: 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can also do that this way:
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.
Too many cats! The world is doomed!
Not many cats! The world is saved!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.

知识点:

  1. 定义函数不能缺少分号。
  2. 变量名不能弄错
  3. 仔细检查,可根据输出结果的错误提示修改代码。
  4. 判断是否等于使用符号“==”。

常见问题

1.我在修复它的时候可以运行代码吗?
你很多时候都需要这么做。计算机就是为了帮你的,所以尽可能多地使用它吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值