Skip to content

Commit 1b0c25c

Browse files
committed
Exercise 26 from LPTHW book
Wednesday, 28 February 2024 2147
1 parent 9c4a88a commit 1b0c25c

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

ex26.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
from sys import argv
2+
#from os.path import exists
23

3-
print("How old are you?", end24=' ')
4+
print('What is your name?', end=' ')
5+
name = input()
6+
print("How old are you?", end=' ')
47
age = input()
58
print("How tall are you?", end=' ')
69
height = input()
710
print("How much do you weigh?", end=' ')
811
weight = input()
912

10-
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
13+
print(f"So, your name is {name}, you're {age} years old, {height} cm tall and {weight} kg heavy.\n")
1114

12-
print("After this you need to open ### Open file and print")
15+
print("After this you need to open file and print\n")
1316

14-
filename = argv
17+
# Opening test.txt
18+
19+
script, filename = argv
1520

1621
txt = open(filename)
17-
print("Here's your file {filename}:")
22+
print(txt.read())
23+
24+
print(f"Here's your file {filename}:")
1825
print(txt.read())
1926

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

2330
txt_again = open(file_again)
31+
indata = txt_again.read()
2432

25-
print(txt_again())
26-
###
33+
print(txt_again.read())
34+
print(f"\nThe input file is {len(indata)} bytes long")
35+
txt.close()
36+
txt_again.close()
37+
#
2738

2839

29-
print('Let\'s practice everything.')
30-
print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')
40+
print('\nLet\'s practice everything.')
41+
print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.\n')
3142

3243
poem = """
3344
\tThe lovely world
@@ -40,11 +51,11 @@
4051

4152
print("--------------")
4253
print(poem)
43-
print("--------------")
54+
print("--------------\n")
4455

4556

4657
five = 4 - 2 + 4 - 1
47-
print(f"This should be five: {five}")
58+
print(f"This should be five: {five}\n")
4859

4960
def secret_formula(started):
5061
jelly_beans = started * 500
@@ -54,38 +65,38 @@ def secret_formula(started):
5465

5566

5667
start_point = 10000
57-
beans, jars = secret_formula(start_point)
68+
beans, jars, crates = secret_formula(start_point)
5869

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

6475
start_point = start_point / 10
6576

6677
print("We can also do that this way:")
67-
formula = secret_formula(startpoint)
78+
formula = secret_formula(start_point)
6879
# this is an easy way to apply a list to a format string
6980
print("We'd have {} beans, {} jars, and {} crates.".format(*formula))
7081

7182

7283

7384
people = 20
74-
cates = 30
85+
cats = 30
7586
dogs = 15
7687

7788

7889
if people < cats:
79-
print ("Too many cats! The world is doomed!")
90+
print ("\n\nToo many cats! The world is doomed!")
8091

8192
if people > cats:
8293
print("Not many cats! The world is saved!")
8394

8495
if people < dogs:
85-
print("The world is drooled on!")
96+
print("Too many dogs! The world is drooled on!")
8697

8798
if people > dogs:
88-
print("The world is dry!")
99+
print("Too many people! The world is dry!")
89100

90101

91102
dogs += 5
@@ -96,6 +107,5 @@ def secret_formula(started):
96107
if people <= dogs:
97108
print("People are less than or equal to dogs.")
98109

99-
100110
if people == dogs:
101111
print("People are dogs.")

testto.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is line 1
2+
This is line 2
3+
This is line 3

0 commit comments

Comments
 (0)