Open woneuy01 opened 4 years ago
The Listener Loop
Inside the while loop there is a function call to get user input. The loop repeats indefinitely, until a particular input is received.
theSum = 0 x = -1 while (x != 0): x = int(input("next number to add up (enter 0 if no more numbers): ")) theSum = theSum + x
print(theSum)
def checkout(): total = 0 count = 0 moreItems = True while moreItems: price = float(input('Enter price of item (0 when done): ')) if price != 0: count = count + 1 total = total + price print('Subtotal: $', total) else: moreItems = False average = total / count print('Total items:', count) print('Total $', total) print('Average price per item: $', average)
checkout()
Subtotal: $ 1.0 Subtotal: $ 3.0 Total items: 2 Total $ 3.0 Average price per item: $ 1.5
def get_yes_or_no(message): valid_input = False while not valid_input: answer = input(message) answer = answer.upper() # convert to upper case if answer == 'Y' or answer == 'N': valid_input = True else: print('Please enter Y for yes or N for no.') return answer
response = get_yes_or_no('Do you like lima beans? Y)es or N)o: ') if response == 'Y': print('Great! They are very healthy.') else: print('Too bad. If cooked right, they are quite tasty.')
Please enter Y for yes or N for no. Please enter Y for yes or N for no. Great! They are very healthy.
1 | initial = 7 |
---|---|
2 | def f(x, y =3, z=initial): // 처음 함수가 정의되었을때 z=7으로 정의되어서 그대로 나간다. 5번줄의 10은 영향 없다. |
3 | print("x, y, z, are: " + str(x) + ", " + str(y) + ", " + str(z)) |
4 | |
5 | initial = 10 |
6 | f(2) |
x, y, z, are: 2, 3, 7
1 | initial = 7 |
---|---|
2 | def f(x, y =3, z=initial): //정의되지 않은 변수는 가장 왼쪽에 놓인다. 정의된것 y=3 가 정의되지 않은 것보다 왼쪽으로 못온다. |
3 | print("x, y, z, are: " + str(x) + ", " + str(y) + ", " + str(z)) |
4 | |
5 | f(2) |
6 | f(2, 5) |
7 | f(2, 5, 8) |
x, y, z, are: 2, 3, 7 x, y, z, are: 2, 5, 7 x, y, z, are: 2, 5, 8
CodeLens: (clens15_1_2) The second tricky thing is that if the default value is set to a mutable object, such as a list or a dictionary, that object will be shared in all invocations of the function. This can get very confusing, so I suggest that you never set a default value that is a mutable object. For example, follow the execution of this one carefully.
1 | def f(a, L=[]): | |
---|---|---|
2 | L.append(a) | |
3 | return L | |
4 | ||
5 | print(f(1)) | |
6 | print(f(2)) | |
7 | print(f(3)) | |
8 | print(f(4, ["Hello"])) | |
9 | print(f(5, ["Hello"])) |
[1] [1, 2] [1, 2, 3] ['Hello', 4] ['Hello', 5]
When the default value is used, the same list is shared. But on lines 8 and 9 two different copies of the list [“Hello”] are provided, so the 4 that is appended is not present in the list that is printed on line 9.
The order in which you pass arguments into the format method matters: the first one is argument 0, the second is argument 1, and so on.
names = ["Jack","Jill","Mary"] for n in names: print("'{}!' she yelled. '{}! {}, {}!'".format(n,n,n,"say hello"))
names = ["Jack","Jill","Mary"] for n in names: print("'{0}!' she yelled. '{0}! {0}, {1}!'".format(n,"say hello"))
'Jack!' she yelled. 'Jack! Jack, say hello!' 'Jill!' she yelled. 'Jill! Jill, say hello!' 'Mary!' she yelled. 'Mary! Mary, say hello!' 'Jack!' she yelled. 'Jack! Jack, say hello!' 'Jill!' she yelled. 'Jill! Jill, say hello!' 'Mary!' she yelled. 'Mary! Mary, say hello!
advfuncs-2-4: What value will be printed for z? initial = 7 def f(x, y = 3, z = initial): print ("x, y, z are:", x, y, z) initial = 0 f(2)
7
def func(args): return ret_val
func = lambda args: ret_val
def f(x): return x - 1
print(f) print(type(f)) print(f(3))
print(lambda x: x-2) print(type(lambda x: x-2)) print((lambda x: x-2)(6))
def last_char(s): return s[-1]
last_char = (lambda s: s[-1])
def stop_at_four(list1): new_list=[] leng=len(list1) n=0 while n<leng: if list1[n] ==4: break new_list.append(list1[n]) n=n+1 return new_list
print(stop_at_four([1,2,3,4,5,6,7,8]))
[1, 2, 3]