Codes for quilting: Difference between revisions
No edit summary |
No edit summary |
||
Line 17: | Line 17: | ||
for word in words: | for word in words: | ||
print(word) | print(word) | ||
# Then, a loop in which we start to play with random again | |||
import random | |||
for word in words: | |||
print(random.choice(words), random.choice(words), random.choice(words), random.choice(words), random.choice(words)) | |||
# How to work with more iterations of the loop? | |||
# For example 100? | |||
# You can use "range" | |||
range? | |||
# Make a loop that starts at 0 and ends at 99 (100 iterations) | |||
import random | |||
for number in range(100): | |||
print(random.choice(words), random.choice(words), random.choice(words), random.choice(words), random.choice(words)) | |||
# You can use range also differently, for example to loop in between two numbers ... | |||
for number in range(50, 60): | |||
print(number, 'x'* number) | |||
# ... or with bigger steps: | |||
for number in range(0,100,10): | |||
print(number, '*'* number) | |||
# Now... write a loop in a loop | |||
for x in range(1,6): | |||
for y in range(1,6): | |||
print('x'* x, 'y'* y) |
Revision as of 16:36, 15 October 2020
loops
for x in range(10):
print ("hello python") print (x)
x = 1 while x<10:
print (f"x is {x}") x = x + 1
.random
words.sort()
words = ['weaving', 'with', 'words', 'and', 'code']
- First a simple loop through the list
for word in words:
print(word)
- Then, a loop in which we start to play with random again
import random for word in words:
print(random.choice(words), random.choice(words), random.choice(words), random.choice(words), random.choice(words))
- How to work with more iterations of the loop?
- For example 100?
- You can use "range"
range?
- Make a loop that starts at 0 and ends at 99 (100 iterations)
import random for number in range(100):
print(random.choice(words), random.choice(words), random.choice(words), random.choice(words), random.choice(words))
- You can use range also differently, for example to loop in between two numbers ...
for number in range(50, 60):
print(number, 'x'* number)
- ... or with bigger steps:
for number in range(0,100,10):
print(number, '*'* number)
- Now... write a loop in a loop
for x in range(1,6):
for y in range(1,6): print('x'* x, 'y'* y)