I'm writing a program in which a range of numbers prints on one line. However my code:
while True:
user = int(input('Enter'))
if user > 0 :
for x in range (0,user +1):
print(x,end=' ')
Has the following output:
Enter9
0 1 2 3 4 5 6 7 8 9 Enter
Why does enter print on the same line? How do I change this?
Use this:
while True:
user = int(input('Enter'))
if user > 0 :
for x in range (0,user +1):
print(x,end=' ')
print()
You have to add just one print() statement after for loop. This makes sure that a line break is added after the elements in the range sequence is printed out.
Your print command does not end with a new line. Add:
print()
after the for loop.
Your code prints "Enter" on the same line because you set the "end" parameter of print to ' '. Therefore, when you print "Enter" you begin in the same line and not in a new one. The default value of "end" is \n meaning that after the print statement it will print a new line.
I'd suggest you to add \n before you print "Enter". This will fix your problem and will be easier to read. Try to change your code to this:
while True:
user = int(input('\nEnter: '))
if user > 0 :
for x in range (0,user +1):
print(x,end=' ')
And you'll see results similar to this:
Enter: 7
0 1 2 3 4 5 6 7
Enter: 8
0 1 2 3 4 5 6 7 8
Enter: 4
0 1 2 3 4
Enter: 1
0 1
Related
I am writing the following code in Python to execute a program that 'skips' out numbers in a range of 1-10 which are divisible by 3:
for i in range(10):
while i % 3 == 0 :
i = i + 1
continue
print(i)
BUT,
the output is printing out duplicate values:
1
1
2
4
4
5
7
7
8
10
Can someone please explain the error in the code?
Thanks.
A program that 'skips' out numbers in a range of 1-10 which are divisible by 3:
for i in range(10):
while i % 3 == 0 :
i = i + 1
continue
print(i)
OUTPUT:
0
1
2
4
5
7
8
Hi #Tech_Nut Welcome to Stackoverflow Since your question states numbers from range 1 to 10 then it rules out 0 as a response in answer also the answer by #Piyush is correct but you should look more into loops and check how they work as for and while can be used together but in different scenarios and not like the range of numbers asked by you.
Correct code to do the required operation is mentioned below, please see if this solves your purpose.
for i in range(10):
if i % 3 == 0:
continue
print(I)
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('')
I use the for loop to input the n words
n = int(input(""))
for i in range(n):
a = input("")
print(a)
when I input:
3
1
1
1
2
It allow me to input the n+1 words
And the n+1 word can not be output
I just want to output n words then equal with the syntax in C:
int a = 0;
for(int i=0; i<n; i++)
scanf("%d",&a);
[Update]
Actually it is a problem with Pycharm. And I don't know why.
In terminal,the code can work.
So,plz not downvote....
I don't understand why this isn't working for you. Try this modified version that makes it clearer what is happening:
n = int(input("Enter number of values: "))
for i in range(n):
a = input("Enter value {} ".format(i+1))
print("Value {0} was {1}".format(i+1, a))
The ouput from this was:
Enter number of values: 3
Enter value 1 1
Value 1 was 1
Enter value 2 1
Value 2 was 1
Enter value 3 2
Value 3 was 2
It ran exactly 3 times when I tried it.
If you want to make it more explicit what you're doing you could set it to for i in range(0,n): but that won't really change anything.
The loop for i in range(n): will run from 0 to n-1.
So if you put in 3 it, it will generate 3 runs, with the values of i being 0, 1, 2.
n = int( input( "Enter the number of runs: " ) )
for item in range( 0, n ):
a = input( "\tPlease Input value for index %d: "%item )
print( a )
It generated the output:
Enter the number of runs: 3
Please Input value for index 0: 1
1
Please Input value for index 1: 1
1
Please Input value for index 2: 1
1
I think you are confusing with the output printed by the loop.
If you enter 3 in the first n = int(input(""))" the loop will go from 0 to 2 (inclusive).
In every loop you ask for a new value of a and print it. So, after the first loop, you input 1 and it outputs 1 (because it prints it). In the second loop you input another 1 and it prints it. Finally you input a 2 and it also prints it.
First loop:
input: 1
output: 1
Second loop:
input: 1
output: 1
Third loop:
input: 2
output: 2
That's why if I run the following
>>> n = int(input(""))
3
>>> for i in range(n):
... a = input("")
... print a
...
1
1
2
2
3
3
I get 6 numbers (inputs and outputs). You can see this more clearly with the following example:
>>> n = int(input("Input: "))
Input: 3
>>> for i in range(n):
... a = input("Input: ")
... print "Output: " + str(a)
...
Input: 1
Output: 1
Input: 2
Output: 2
Input: 3
Output: 3
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!
I have this strange problem when following a reference, this code:
for r in range(10):
for c in range(r):
print "",
for c in range(10-r):
print c,
print ""
should print out something like this:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
but Instead I am getting this:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Can anyone explain to me what is causing in indent on right side, it seems so simple but I have no clue what I can do to fix this?
You were printing the leading spaces incorrectly. You were printing empty quotes ("") which is printing only a single space. When you do print c, there is a space printed after c is printed. You should print " " instead to get the correct spacing. This is a very subtle thing to notice.
for r in range(10):
for c in range(r):
print " ", #print it here
for c in range(10-r):
print c,
print ""
Test
If you want to format it just so, it might be better to just let Python do it for you instead of counting explicit and the hidden implicit spaces. See the string formatting docs for what {:^19} means and more.
for i in range(10):
nums = ' '.join(str(x) for x in range(10 - i))
#print '{:^19}'.format(nums) # reproduces your "broken" code
print '{:>19}'.format(nums) # your desired output
Using the print function is a good alternative sometimes, as you can eliminate hidden spaces by setting the keyword argument end to an empty string:
from __future__ import print_function # must be at the top of the file.
# ...
print(x, end='')
You are simply not creating enough indent on the left side (there is no such thing as right side indent while printing).
For every new line you want to increase the indent by two spaces, because you are adding a number+whitespace on the line above. "", automatically adds one whitespace (this is why there is whitespaces between the numbers). Since you need to add two, simply add a whitespace within the quotes, like this: " ",.
The extra whitespace is filling the space of the number in the line above. The comma in "", is only filling the space between the numbers. To clarify: " ", uses the same space as c,, two characters, while "", only uses one character.
Here is your code with the small fix:
for r in range(10):
for c in range(r):
print " ", # added a whitespace here for correct indent
for c in range(10-r):
print c,
print ""