Skip to content

Commit cc8d65a

Browse files
Caleb CurryCaleb Curry
authored andcommitted
input to list
1 parent d4ed35e commit cc8d65a

2 files changed

Lines changed: 112 additions & 3 deletions

File tree

‎beginner_python/02-strings.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@
6262
print("""Name: Caleb
6363
Age: 58""")
6464

65-
#skip newline using / (without it, it would go down a line)
65+
#skip newline using \ (without it, it would go down a line each line)
6666
print("""\
67-
Name: Caleb
67+
Name: Caleb. \
6868
Age: 58""")
6969

7070
"""You may see
@@ -189,7 +189,7 @@
189189

190190
#GOOD
191191
length = len(name)
192-
msg = "length is " + str(length))
192+
msg = "length is " + str(length)
193193
print(msg)
194194

195195
#EVEN BETTER

‎python/03-list-input.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
########## SPLIT ##########
2+
3+
msg = "Pay attention to each word that I say..."
4+
words = msg.split() #returns list
5+
print(words)
6+
7+
msg = "this,is,important,data" #How to parse CSV
8+
print(msg.split(","))
9+
10+
11+
########## SPLIT STRING BY LINE ##########
12+
13+
14+
#This may have came from a file, for example.
15+
msg = """\
16+
Hey there.
17+
My name is Caleb!
18+
What's your name?
19+
You're name is NERD? Weird...
20+
Bye for now!"""
21+
22+
print(msg) #to see how it is stored...
23+
24+
print(msg.split('\n'))
25+
26+
########## INPUT USING SPLIT ##########
27+
28+
print("List your favorite foods separated by ', '")
29+
print("Example input: ")
30+
print("Kale, bok choy, brussel sprouts")
31+
32+
foods = input().split(', ')
33+
34+
for food in foods:
35+
print("You said " + food)
36+
37+
#an obvious downfall is that this is very touchy.
38+
#instead, we could ask one food per line
39+
40+
########## LOOPING TO GET USER INPUT ##########
41+
42+
fav_foods = []
43+
while True:
44+
print("Enter a food. q to quit: ", end="")
45+
fav = input()
46+
if str.lower(fav) == 'q':
47+
break
48+
fav_foods.append(fav)
49+
50+
print("all foods:", fav_foods)
51+
52+
53+
########## LIST AS STACK ##########
54+
55+
#Consider a stack of plates.
56+
#The last one you add is the first to be removed.
57+
58+
#The data structure depends on adding to the end of a list:
59+
stack = []
60+
stack.append("added")
61+
#and removing from the end of the list
62+
stack.pop()
63+
64+
fav_foods = []
65+
while True:
66+
print("Enter a food. q to quit, r to remove: ", end="")
67+
fav = input()
68+
if str.lower(fav) == 'q':
69+
break
70+
if str.lower(fav) == 'r':
71+
popped = fav_foods.pop()
72+
print("removed " + popped)
73+
print("all foods:", fav_foods)
74+
continue
75+
fav_foods.append(fav)
76+
print("all foods:", fav_foods)
77+
78+
print("final foods:", fav_foods)
79+
80+
81+
########## QUEUE VARIATION ##########
82+
83+
84+
#The difference with a queue is that the first added is the first removed
85+
#consider a line to a roller coaster
86+
#first in line rides ride first.
87+
88+
#The data structure depends on adding to the end of a list:
89+
stack = []
90+
stack.append("added")
91+
#and removing from the FRONT of the list
92+
stack.pop(0) #remove index 0
93+
94+
fav_foods = []
95+
while True:
96+
print("(QUEUE) Enter a food. q to quit, eat to remove: ", end="")
97+
fav = input()
98+
if str.lower(fav) == 'q':
99+
break
100+
if str.lower(fav) == 'eat':
101+
popped = fav_foods.pop(0)
102+
print("removed " + popped)
103+
print("all foods:", fav_foods)
104+
continue
105+
fav_foods.append(fav)
106+
print("all foods:", fav_foods)
107+
108+
print("final foods:", fav_foods)
109+

0 commit comments

Comments
 (0)