world!
I'm stuck at a basic question.
We're using simple commands for these questions (format, if, while, and all basics).
I came as far as to be able to produce this:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
by using the following code:
number= 0
while number<= 0:
number = input("Give a number which is bigger than 0 : ")
if number.isdigit():
number=int(number)
else:
print("Give an integer")
number= 0
for x in range(number):
for y in range(1,number+1):
print(" {}{} ".format('',y), end='' )
print('')
The problem comes with the next question:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Unfortunately I'm stuck at being able to change the code so it will follow the pattern shown above.
Thanks in advance!
You just need to take a new variable and increment it in every iteration:
number= 0
while number<= 0:
number = input("Give a number which is bigger than 0 : ")
if number.isdigit():
number=int(number)
else:
print("Give an integer")
number= 0
z=0
for x in range(number):
for y in range(1,number+1):
z += 1
print(" {}{:<3} ".format('',z), end='' )
print('')
Output:
>>>
Give a number which is bigger than 0 : 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
You can also do it in for loop instead of two:
for i in range(number*number):
i+=1
print(" {}{:<3} ".format('',i), end='' )
if i%number==0:
print('')
Related
I am trying to create ruler by print, it should look like this for input value 5:
Im trying to change in my code numbers to symbols, my code is:
length = str(input("Enter the ruler length = "))
def ruler(string):
top = []
top_out = []
bottom = []
for i in range(length):
top.append((i+1)//10)
bottom.append((i+1)%10)
for i in range(length):
if ((i+1)//10) == 0:
top_out.append(" ")
elif (((i+1)//10) in list(sorted(set(top)))) and (((i+1)//10) not in top_out):
top_out.append(((i+1)//10))
else:
top_out.append(" ")
print (''.join(list(map(str, top_out))))
print (''.join(list(map(str,bottom))))
print (string)
How to correct it to get appropriate output format of a ruler?
Ruler printing can be down by pretty small function like this,
def print_ruler(n):
print('|....'*(n)+'|')
print(''.join(f'{i:<5}' for i in range(n+1)))
Execution:
In [1]: print_ruler(5)
|....|....|....|....|....|
0 1 2 3 4 5
In [2]: print_ruler(10)
|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10
In [3]: print_ruler(15)
|....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
For double-digit numbers, It doesn't come to the center.
For ex: For 12, | align with number 1 or 2 it can't not make into the center of 12
I need to take number x as input and print the first x odd numbers. If input 8 was given, the output would be: 1 3 5 7 9 11 13 15.
x = int(input('Enter your number:'))
for i in range(2*x):
if i % 2 == 1:
print(i)
Here is a solution without loop. It uses range to get directly the even numbers, converts those integers to string and displays them all at once using newlines as separator:
n = int(input('Enter your number:'))
print('\n'.join(map(str,range(1,2*n,2))))
output for 8 as input:
1
3
5
7
9
11
13
15
I'm attempting to write a program called multChart(x,y) that prints a multiplication table based on two inputs, one specifying the number of rows to print and another specifying the number of columns. So it would look like this:
>>> multChart(4,5):
1: 1 2 3 4 5
2: 2 4 6 8 10
3: 3 6 9 12 15
4: 4 8 12 16 20
Here's what my current code looks like:
def multChart(x,y):
for i in range(1,x+1):
print(i,':',i*1,i*2,i*3,i*4,i*5)
I'm totally stuck on how to implement the y value. I also know there should be a better way of printing the multiplication instead of i * multiples of five, but I'm not sure what loop to use. Any help would be greatly appreciated.
You need another loop inside your print for looping over the y range:
def multChart(x, y):
for i in range(1, x+1):
print(i, ':', *[i * z for z in range(1, y+1)])
def multChart(x,y):
for i in range(1,x+1):
print(i, ':', end=" ")
for j in range(1,y+1):
print(i*j, end =" ")
print()
multChart(4,5)
produces
1 : 1 2 3 4 5
2 : 2 4 6 8 10
3 : 3 6 9 12 15
4 : 4 8 12 16 20
You can use a second for loop for the second index. Also, note that you can use end in the print statement.
def multChart(x,y):
for i in range(1,x+1):
print(i,':',*list(map(lambda y: i*y,list(range(1,y+1 ) ) ) ) )
multChart(4,5)
this is my code right now:
loop_count = 1
for i in range(mystery_int):
for x in range(1,mystery_int):
print(x*loop_count, end=" ")
print (loop_count)
loop_count+=1
this is what it is supposed to print:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
But it prints:
1 2 3 4 1
2 4 6 8 2
3 6 9 12 3
4 8 12 16 4
5 10 15 20 5
You need to range till mystery_int + 1 because in range, second argument is exclusive. So, for example, range(1,6) gives numbers from 1 to 5.
Also, I added an empty print() which basically adds a newline to match with desired output.
Using end='\t' further aligns output properly.
loop_count = 1
mystery_int = 5
for i in range(mystery_int):
for x in range(1, mystery_int + 1):
print(x * loop_count, end='\t')
print()
loop_count += 1
the range for x should be range(1,mystery_int+1), and you also incorrectly print loop_count at the end of each line (which I replaced with the empty string, just to produce a newline).
loop_count = 1
for i in range(mystery_int):
for x in range(1,mystery_int+1):
print(x*loop_count, end=" ")
print('')
loop_count+=1
Note that the loop_count variable is not really needed. You could write the program as:
for i in range(1,mystery_int+1):
for x in range(1,mystery_int+1):
print(x*i, end=" ")
print('')
or even better as:
for i in range(1,mystery_int+1):
print(*[x*i for x in range(1,mystery_int+1)], sep=" ")
you are running on two for loops in addition to using another counter, i would recommend sticking only to the loops:
for i in range(1,mystery_int+1):
for x in range(1,mystery_int+1):
print(i*x, end=" ")
print("") # new line
1 first_num = raw_input("Please input first number: ")
2 sec_num = raw_input("Please input second number: ")
3
4 answer = int(first_num) + int(sec_num)
5
6 print "Now I will add your two numbers: ", answer
7
8 print "Pretty cool, huh?"
9
10 print "Now I'll count backwards from ", answer
11
12 counter = answer
13
14 while (counter >= 0):
15 print counter
16 counter = counter - 1
17
18 print "All done!"
This code does roughly what the print messages say it does. It takes two numbers, adds them together, and then prints a counter from the sum down to 0.
Please input first number: 2
Please input second number: 3
Now I will add your two numbers: 5
Pretty cool, huh?
Now I'll count backwards from 5
5
4
3
2
1
0
All done!