woneuy01 / python2

0 stars 0 forks source link

while #4

Open woneuy01 opened 4 years ago

woneuy01 commented 4 years ago

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]

woneuy01 commented 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.

woneuy01 commented 4 years ago

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)

woneuy01 commented 4 years ago

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

woneuy01 commented 4 years ago

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.

woneuy01 commented 4 years ago
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

woneuy01 commented 4 years ago
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

woneuy01 commented 4 years ago

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.

woneuy01 commented 4 years ago

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.

this works

names = ["Jack","Jill","Mary"] for n in names: print("'{}!' she yelled. '{}! {}, {}!'".format(n,n,n,"say hello"))

but this also works!

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!

woneuy01 commented 4 years ago

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

woneuy01 commented 4 years ago

def func(args): return ret_val

func = lambda args: ret_val

woneuy01 commented 4 years ago

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))


2 > 4
woneuy01 commented 4 years ago

def last_char(s): return s[-1]

last_char = (lambda s: s[-1])