练习26--恭喜你,来做个测试吧

引以为戒:       
“有些程序员会宣称他们的代码很完美,这些人一般比较蠢,很少考虑别人的感受。好的程序员会像科学家一样,假设他们的代码总是存在一定概率是错的。好的程序员一般会在软件出现问题的情况下,用所有可能的方式排查自己会犯的错误,直到最后得出结论可能真的是其他人的代码出了问题。”
1 代码内容:
更改后的代码:
  1 from sys import argv
  2 # -*- coding:utf-8 -*-
  3 
  4 
  5 
  6 print("How old are you?", end=' ')
  7 age = input()
  8 print("How tall are you?", end=' ')
  9 height = input()
 10 print("How much do you weigh?", end=' ')
 11 weight = input()
 12 
 13 print(f"So, you're {age} old, {height} tall and {weight} heavy.")
 14 
 15 
 16 
 17 script, filename = argv
 18 
 19 txt = open(filename)
 20 
 21 print("Here's your file {filename}:")
 22 print(txt.read())
 23 
 24 print("Type the filename again:")
 25 file_again = input("> ")
 26 
 27 txt_again = open(file_again)
 28 
 29 print(txt_again.read())
 30 txt_again.close()
 31 
 32 
 33 
 34 print('Let\'s practice everything.')
 35 print('You\'d need to know \'bout escapes \
 36       with \\ that do \n newlines and \t tabs.')
 37 
 38 poem = """
 39 \tThe lovely world
 40 with logic so firmly planted
 41 cannot discern \n the needs of love
 42 nor comprehend passion from intuition
 43 and requires an explanation
 44 \n\t\twhere there is none.
 45 """
 46 
 47 print("--------------")
 48 print(poem)
 49 print("--------------")
 50 
 51 
 52 
 53 five = 10 - 2 + 3 - 6
 54 print(f"This should be five: {five}")
 55 
 56 
 57 
 58 def secret_formula(started):
 59     jelly_beans = started * 500
 60     jars = jelly_beans / 1000
 61     crates = jars/100
 62     return jelly_beans, jars, crates
 63 
 64 start_point = 10000
 65 beans, jars,crates= secret_formula(start_point)
 66 
 67 # remember that this is another way to format a string
 68 print("With a starting point of: {}".format(start_point))
 69 # it's just like with an f"" string
 70 print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
 71 
 72 start_point = start_point / 10
 73 
 74 print("We can also do that this way:")
 75 formula = secret_formula(start_point)
 76 # this is an easy way to apply a list to a format string
 77 print("We'd have {} beans, {} jars, and {} crates.".format(*formula))
 78 
 79 
 80 
 81 people = 20
 82 cats = 30
 83 dogs = 15
 84 
 85 if people < cats:
 86     print ("Too many cats! The world is doomed!")
 87 
 88 if people > cats:
 89     print("Not many cats! The world is saved!")
 90 
 91 if people < dogs:
 92     print("The world is drooled on!")
 93 
 94 if people > dogs:
 95     print("The world is dry!")
 96 
 97 dogs += 5
 98 
 99 if people > dogs:
100     print("People are greater than or equal to dogs.")
101 
102 if people < dogs:
103     print("People are less than or equal to dogs.")
104 
105 if people == dogs:
106     print("People are dogs.")

我在改错过程中中没有检查到的地方是:定义函数过程和if条件判断后面的冒号、if语句的判断条件、自己改的过程中引入了拼写错误、有一个地方的拼写错误没有察觉到(start_point)以及height没有被定义,还是不够细心。

2 运行结果

 1 PS E:\3_work\4_python\2_code_python\02_LearnPythonTheHardWay> python ex26.py test.txt
 2 How old are you? 16
 3 How tall are you? 162
 4 How much do you weigh? 45
 5 So, you're 16 old, 162 tall and 45 heavy.
 6 Here's your file {filename}:
 7 I
 8 Love
 9 You
10 
11 Type the filename again:
12 > test.txt
13 I
14 Love
15 You
16 
17 Let's practice everything.
18 You'd need to know 'bout escapes       with \ that do
19  newlines and    tabs.
20 --------------
21 
22         The lovely world
23 with logic so firmly planted
24 cannot discern
25  the needs of love
26 nor comprehend passion from intuition
27 and requires an explanation
28 
29                 where there is none.
30 
31 --------------
32 This should be five: 5
33 With a starting point of: 10000
34 We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
35 We can also do that this way:
36 We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.
37 Too many cats! The world is doomed!
38 The world is dry!
39 People are dogs.

在运行过程中命令行参数没有给到test.txt文件也报错了。

posted @ 2020-07-10 10:39  洛兰123  阅读(190)  评论(0编辑  收藏  举报