This is the exercise I need to complete:
Using nested loops, write some code that outputs the following:
##########
**********
##########
**********
##########
Following is all the code I have so far. I assume I need to combine these two loop functions to one but I'm struggling with it. I'm early on in my first class and any help with this would be appreciated!
for a in range (0, 5, 2):
for b in range(10):
print("#", end = "")
print("")
for a in range (1, 5, 2):
for b in range(10):
print("*", end = "")
print("")
Since no input is specified, only a fixed output:
for _ in '_':
for _ in '_':
print('''##########
**********
##########
**********
##########''')
And yes, if that was my homework exercise, I'd absolutely submit this.
There are several ways to do it. First you should check how many lines you need to output. 5, so you need a loop doing something 5 times.
for i in range(5):
Now you need 2 loops to paste print the 2 line patterns (you already have them in your code)
for b in range(10):
print("#", end = "")
print("")
and
for b in range(10):
print("*", end = "")
print("")
If you need to alternate between 2 values it's mostly the best to use the Modulo Operator.
So you can just switch between the 2 loops by % 2.
for i in range(5):
if i % 2 == 0:
for b in range(10):
print("#", end = "")
print("")
else:
for b in range(10):
print("*", end = "")
print("")
I would suggest to simply base the symbol on the row index, even or odd
nb_lines = 5
line_size = 10
for i in range(nb_lines):
for _ in range(line_size):
if i % 2 == 0:
print("#", end="")
else:
print("*", end="")
print()
But no need of nested loops
nb_lines = 5
line_size = 10
for i in range(nb_lines):
if i % 2 == 0:
print("#" * line_size)
else:
print("*" * line_size)
def printStripes(row_length, number_of_rows):
for _ in '_':
print( "".join(
[ "\n" if n%(row_length+1)==row_length else
"#" if (n//(row_length+1))%2==0 else
"*"
for n in range(number_of_rows*(row_length+1))
]
),end="")
printStripes(row_length=5,number_of_rows=8)
But don't tell your teacher that you got if from stackoverflow. (Thanks Kelly, for how to deal with the nested loop constraint.)
You can use this to get the result that you want
for i in range(0,5):
print("*"*10)
print("#"*10)
Related
Why does my code not work in the 2nd iteration? I want to give input in row = 3 or 5 or x, and then I expect it to produce the following output pattern:
#When row = 3
1|. . #
2|. ##
3|###
Please note that the number of "." will be opposite to the number of "#"
The i loop prints a new line, the j loop prints "#", and the k loop prints "."
row = 3
for i in range(1, row + 1):
for j in range(1, i + 1):
for k in range(1, (row - i) + 1):
print(".", end= "")
print("#", end="")
print("")
Your k loop runs too many times to do what you want after the first iteration.
However I think this does what you want, hope it helps:
row = 3
n_hash = 0
n_dots = row
for i in range(row):
print(f'{i+1}|{n_dots * "."}{n_hash * "#"}')
n_dots -= 1
n_hash += 1
How to print the pattern like this:
(need to print n-1 lines)
input=3
----#
--#-#-#
input=6
----------#
--------#-#-#
------#---#---#
----#-----#-----#
--#-------#-------#
My code:
row = int(input())
for i in range(1, row):
for j in range(1,row-i+1):
print("-", end="")
for j in range(1, 2*i):
if j==1 or j==2*i-1:
print("#", end="")
else:
print("-", end="")
print()
MY OUTPUT:
input=5
----#
---#-#
--#---#
-#-----#
Please explain how to do??
There are a few things missing and to be improved in your code:
There's no need to make a loop to print the same character again and again: on python you can use the product to repeat the character an x number of times. For example: "-" * 3 == "---"
They way you calculate the hyphens in the middle is fine, but you need to do it twice and add an "#" in between.
You can build the strings part by part first and then print the whole line, avoiding having to print an empty line in the end of the loop.
Personally, since the first line is going to have one "#" and not three, I prefer to calculate it and print it separately.
With these improvements, a solution to your problem could be:
row = int(input())
print("-" * (row - 1) * 2 + "#")
for i in range(row - 2, 0, -1):
left_hyphens = "-" * i * 2
mid_hyphens = "-" * (1 + 2 * (row - 2 - i))
print(left_hyphens + "#" + mid_hyphens + "#" + mid_hyphens + "#")
row = int(input())
for i in range(1, row):
for j in range(1,2*(row-i)+1):
print("-", end="")
for j in range(1, 4*i):
if j==1 or j==2*i-1 or j==4*i-3:
print("#", end="")
elif j<=4*i-3:
print("-", end="")
print()
I have following code, which should print an output of k numbers around n number, and instead the n number the code should replace by *:
def numbers_around(n, k):
for cislo in range(n-k, n+k+1):
if cislo == n:
print("*", end=" ")
else:
print(cislo, end=" ")
numbers_around(8, 3)
numbers_around(10, 2)
The output should be:
5 6 7 * 9 10 11
8 9 * 11 12
But my output is all in one line:
5 6 7 * 9 10 11 8 9 * 11 12
You are using the parameter end = " " in the print function, which replaces the default end used by python which is starting new line; if you want to start new line after each printing, just add print() at the end.
the full function should be
def numbers_around(n, k):
for cislo in range(n-k, n+k+1):
if cislo == n:
print("*", end=" ")
else:
print(cislo, end=" ")
print()
Using end=" " tells the print function to output a space at the end, instead of the line break that it would normally output.
Therefore the output of the second function call starts on the same line as the output of the first function call ends.
You need to output an additional line break at the end of the numbers_around function.
To do that, just add an empty print() after the for loop:
def numbers_around(n, k):
for ...:
# ...
print()
The problem is that you're telling print() to put a space at the end instead of the newline character '\n' it normally uses. You could fix this by just adding another call to print() at the end, but actually, if you redesign the function a little bit, you can make it a lot simpler. By using the splat operator, you can print everything at once.
Option 1: Use a generator expression or list comprehension instead of a for-loop.
def numbers_around(n, k):
r = range(n-k, n+k+1)
out = ('*' if i==n else i for i in r)
print(*out)
Option 2: Instead of replacing n, get the two ranges around n and put a '*' in the middle.
def numbers_around(n, k):
r1 = range(n-k, n)
r2 = range(n+1, n+k+1)
print(*r1, '*', *r2)
I have written for a coding problem assigned to me essentially there is a scratch and win ticket and I have to figure out what prizes I could win. I will be given 10 inputs and each output after running through should be on its own line. However Every time I run the code with 10 inputs all of the inputs just appear on one line.
How do I make sure that each output appears on its own line?
Although to be noted my code does output the correct answer its just the formatting that is troubling I want each answer to be on its own line instead of just being on one line.
from collections import Counter
def Winner(L):
zz = Counter(L)
if zz["63"] == 9:
print("No Prizes Possible")
elif zz["63"] == 0:
for T in zz:
if zz[T] >= 3:
print("$" + T, end=" ")
else:
for V in zz:
KK = zz[V] + zz["63"]
if KK >= 3:
if V != "63":
print("$" + V, end=" ")
for d in range(10):
z = [input() for i in range(9)]
yy = []
for g in z:
if g == "?":
yy.append(ord(g))
else:
yy.append(g.replace("$", ""))
yy = [int(i) for i in yy]
yy.sort()
yy = [str(i) for i in yy]
Winner(yy)
Here is a sample input of what I mean:
$10
$100
?
$10
$1
$50
$50
$1000
$1
essentially having 10 inputs like these.
If you add \n (the newline character) at the end of your string, you will add a newline after each time your program prints. For example, you could have this loop which would print "hello world" 10 times, each on its own separate line.
for i in range (10):
print('hello world', '\n')
Or more conveniently, you can get rid of the end = " ", and Python will automatically add a new line.
You are using end=" " in your print function.
Just leave it as print("$" + T) and print("$" + V) so it appends newline by default.
Hi I'm wondering on how to use a nested loop to draw this pattern on the output
##
# #
# #
# #
# #
# #
# #
# #
I found out how to do it in a loop without nested, but I am curious as to how to draw this using a nested while loop.
while r < 7:
print("#{}#".format(r * " "))
r = r + 1
Here is an answer to your actual question: using two nested while loops.
num_spaces_wanted = 0
while num_spaces_wanted < 7:
print('#', end='')
num_spaces_printed = 0
while num_spaces_printed < num_spaces_wanted:
print(' ', end='')
num_spaces_printed += 1
print('#')
num_spaces_wanted += 1
As the print statements show, this is for Python 3.x. Adjust them for 2.x or add the line from __future__ import print_function to get the 3.x style printing.
If you intend to do this in python
You don't need a nested loop.
Edit With two loops
#!/bin/python
import sys
n = int(raw_input().strip())
for i in xrange(n):
sys.stdout.write('#')
for j in xrange(i):
sys.stdout.write(' ')
sys.stdout.write('#')
print
There are a number of other answers which already correctly answer the question, but I think the following does so in a conceptually more simple way, and it should make it easier to learn from.
spaces = 0
while spaces < 8:
to_print = "#"
count = 0
while count < spaces:
to_print += " "
count += 1
to_print += "#"
print to_print
spaces += 1
The most efficient solution for a nested loop:
#!/bin/python
n = int(raw_input().strip())
for i in xrange(n):
string = "#" + i * " " + "#"
print string
for -1 in xrange(n)
# Do nothing