笨办法学Python3 习题26 恭喜你,现在可以考试了!

下载代码learnpythonthehardway.org/python3/exercise26.txt

进行修改

 1 print("How old are you?", end=' ')
 2 age = input()
 3 print("How tall are you?", end=' ')
 4 height =input()                         # 没有input()
 5 print("How much do you weigh?", end=' ')# 没加括号
 6 weight = input()
 7 
 8 print(f"So, you're {age} old, {height} tall and {weight} heavy.")
 9 
10 from sys import argv                    # 没有导入模块
11 script, filename = argv
12 
13 txt = open(filename)                    # filename没拼写对
14 
15 print(f"Here's your file {filename}:")  # 没有加f
16 print(txt.read())                       # txt少t
17 
18 print("Type the filename again:")
19 file_again = input("> ")
20 
21 txt_again = open(file_again)
22 
23 print(txt_again.read())
24 
25 
26 print("Let's practice everything.")                      #里面有单引号,外面就用双引号
27 print("""You\'d need to know \'bout escapes 
28       with \\ that do \n newlines and \t tabs.""")       #没用三对引号
29 
30 poem = """
31 \tThe lovely world
32 with logic so firmly planted
33 cannot discern \n the needs of love
34 nor comprehend passion from intuition
35 and requires an explanation
36 \n\t\twhere there is none.
37 """
38 
39 print("--------------")                     # 特殊符号也要用引号
40 print(poem)
41 print("--------------")
42 
43 
44 five = 10 - 2 + 3 - 6
45 print(f"This should be five: {five}")       # 少半个括号
46 
47 def secret_formula(started):                # 函数没有加冒号
48     jelly_beans = started * 500
49     jars = jelly_beans / 1000
50     crates = jars / 100                     # 少写/ 运算符
51     return jelly_beans, jars, crates
52 
53 
54 start_point = 10000
55 beans, jars ,crates= secret_formula(start_point)  # 左边少写一个变量
56 
57 # remember that this is another way to format a string
58 print("With a starting point of: {}".format(start_point))
59 # it's just like with an f"" string
60 print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
61 
62 start_point = start_point / 10
63 
64 print("We can also do that this way:")
65 formula = secret_formula(start_point)
66 # this is an easy way to apply a list to a format string
67 print("We'd have {} beans, {} jars, and {} crates.".format(*formula))
68 
69 
70 
71 people = 20
72 cats = 30
73 dogs = 15
74 
75 
76 if people < cats:
77     print ("Too many cats! The world is doomed!") # 没加括号
78 
79 if people > cats:                                 # < 改成 >
80     print("Not many cats! The world is saved!")
81 
82 if people < dogs:
83     print("The world is drooled on!")
84 
85 if people > dogs:                                 # 没加冒号
86     print("The world is dry!")
87 
88 
89 dogs += 5
90 
91 if people >= dogs:
92     print("People are greater than or equal to dogs.")
93 
94 if people <= dogs:                                  # 没加冒号
95     print("People are less than or equal to dogs.") # 没加引号
96 
97 
98 if people == dogs:                                  # 判断是否相等要用== 而不是= 这是赋值的意思
99     print("People are dogs.")
PS C:\Users\Administrator\lpthw> python ex26.py test20.txt
How old are you? 23
How tall are you? 164
How much do you weigh? 53
So, you're 23 old, 164 tall and 53 heavy.
Here's your file test20.txt:
This is line
This is line
This is line
Type the filename again:
> test20.txt
This is line
This is line
This is line
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!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
PS C:\Users\Administrator\lpthw>

 

posted @ 2023-10-08 23:18  萹豆  阅读(24)  评论(0编辑  收藏  举报