티스토리 뷰

IT/Python: Codecademy

8 Loop

Last72 2018. 2. 28. 21:26
count = 0

while count < 10: # Add a colon
  print count
  count += 1
# Increment count
//

count = 0

while True:
  print count
  count += 1
  if count >= 10:
    break
//

from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
# Start your game!
while guesses_left > 0:
  guess = int(raw_input("Your guess: "))
  if guess == random_number:
    print "You win!"
    break
  guesses_left -= 1
else:
  print "You lose!"
//

hobbies = []

# Add your code below!
for a in range(3):
    i = raw_input('Select a hobbie')

    hobbies.append(i)
print hobbies
//

choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:'
for index, item in enumerate(choices):
  print index + 1, item
//

list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]

for a, b in zip(list_a, list_b):
  # Add your code here!
    print max(a, b)
//

fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']

print 'You have...'
for f in fruits:
  if f == 'tomato':
    print 'A tomato is not a fruit!' # (It actually is.)
    break
  print 'A', f
else:
  print 'A fine selection of fruits!'


And, bunch of excercises. answers are below.

https://github.com/ummahusla/Codecademy-Exercise-Answers/tree/master/Language%20Skills/Python

'IT > Python: Codecademy' 카테고리의 다른 글

10 Advanced Topics in Python  (0) 2018.03.07
9 Exam Statistics  (0) 2018.03.06
7 Lists and Functions  (0) 2018.02.26
6 Student Becomes the Teacher  (0) 2018.02.24
5 Lists & Dictionaries  (0) 2018.02.22
댓글
Announcement
Recent Posts
Recent Comments
Total
Today
Yesterday
Link
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Search by month