I have an assignment to write odd number - python

I wanted to print odd numbers as many as input.
Example: input=7
Output: 1,3,5,7,9,11,13
This is what i wrote
a=int(input("input how many odd numbers to print: "))
for i in range(a):
if(i%2==1):
print(i)
Yes, the output is obviously
1
3
5
And i tried this too
a=int(input("input ho many odd numbers to print: "))
i=1
while(i<=a):
if(i%2==1):
print(i)
The output is infinite loop printing the input number

a=int(input("input how many odd numbers to print: "))
for i in range(1,a*2,2):
print(i)

If you really want to use a while loop
i = 1
counter = 0
while counter < a:
if i % 2 == 1:
print(i)
counter += 1
i += 1

In the while-loop, the variable i never changes, so you're just reprinting the same number over and over again. Try this:
while(i<=a):
if(i%2==1):
print(i)
i += 1

Related

Why does it not work when I define the range of my while loop with a variable?

I'm a complete novice at python and am trying to list the first n positive numbers, where n is an inputted value. E.g. when n=5, I want to output 5, 4, 3, 2 and 1.
This is my code which doesn't work:
n= int(input("Please enter a number: "))
i=0
while i<n:
print(n)
n=n-1
i=i+1
I know I can answer the question with this code:
n= int(input("Please enter a number: "))
i=n
while i>0:
print(i)
i=i-1
but would like to understand why what I tried at the beginning is not working.
You are decrementing the number and incrementing the counter at the same time.
For example:
n
i
i<n
5
0
True
4
1
True
3
2
True
2
3
False
The loop exits once False is encountered. To solve this you need to only increment i and use print(n-i), keep the i<n comparison, and remove the 'n=n-1' line
n= int(input("Please enter a number: "))
i=0
while i<n:
print(n-i)
i=i+1
Because, you are decreasing n and increasing i, so at some point the while condition will not satisfy anymore, it has got nothing to do with python.

I am very new to programming, and this is my first python script assignment. Can someone please tell me what i am doing wrong? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The program is about giving out the sum of odd and even numbers separately from 1 upto given n term.
INPUT
print()
print("Program to display sum of n terms of odd/even natural numbers!")
print()
num = int(input("Enter the number of natural numbers: "))
even_total = 0
odd_total = 0
i = 1
while i == num:
if(i % 2 == 0):
even_total = even_total + i
i += 1
else:
odd_total = odd_total + i
i += 1
print()
print("The sum of even numbers from 1 to {0} = {1}".format(i, even_total))
print("The sum of odd numbers from 1 to {0} = {1}".format(i, odd_total))
OUTPUT:
Program to display sum of n terms of odd/even natural numbers!
Enter the number of natural numbers: 10
The sum of even numbers from 1 to 1 = 0
The sum of odd numbers from 1 to 1 = 0
>>>
Your program has not completed the intended interations of the loop, which would have been more obvious if you had put some additional print statements inside the loop (e.g. print(i)) - this is a simple debugging technique that you can use in future. Although in fact there is a clue in the output that you see, where it says from 1 to 1 rather than something like from 1 to 10.
What is happening is that the first time the while i == num: is tested, it evaluates False (0 is not equal to 10), so the loop is never entered. If you change the == to <= here, then this will mostly solve the problem (the loop will go up to and including 10).
Other improvements that you can make will include:
In the print statements at the end, use num instead of i:
print("The sum of even numbers from 1 to {0} = {1}".format(num, even_total))
Inside the loop, the i += 1 is done in both the if and else blocks, so instead you could do a single unconditional i += 1 after the if...else...
if i % 2 == 0:
even_total += i
else:
odd_total += i
i += 1
(I've also suggested using += here with the totals, like you are already doing with i.)
You can also use a for loop using range instead, and then you don't need to explicitly increment i at all. Note that the upper limit of the range has to be one greater than the value of i on the last iteration.
for i in range(1, num + 1):
if i % 2 == 0:
even_total += i
else:
odd_total += i
You need to replace while i==num with while i<=num
print()
print("Program to display sum of n terms of odd/even natural numbers!")
print()
num = int(input("Enter the number of natural numbers: "))
even_total = 0
odd_total = 0
i = 1
while i <= num:
if(i % 2 == 0):
even_total = even_total + i
else:
odd_total = odd_total + i
i += 1 #a small optimization to reduce number of lines
print()
print("The sum of even numbers from 1 to {0} = {1}".format(i-1, even_total))
print("The sum of odd numbers from 1 to {0} = {1}".format(i-1, odd_total))
Since, while exiting the while loop your i will be greater than num, you should print i-1 in the print statements that follow the loop.
I input 4 and got the following output:
Program to display sum of n terms of odd/even natural numbers!
Enter the number of natural numbers: 4
The sum of even numbers from 1 to 5 = 6
The sum of odd numbers from 1 to 5 = 4
The condition for the loop is the problems. It should be:
while i<= num:
You are using thiswhile i==num: which means that when the loop run at first it will take num=1 which means that your while i==1 is true and the while loop breaks. So as farther it can't do the sum.
So you can use while<=num
Here is your code
print()
print("Program to display sum of n terms of odd/even natural numbers!")
print()
num = int(input("Enter the number of natural numbers: "))
even_total = 0
odd_total = 0
i = 1
while i <= num:
if(i % 2 == 0):
even_total = even_total + i
else:
odd_total = odd_total + i
i += 1
print()
print("The sum of even numbers from 1 to {0} = {1}".format(num, even_total))
print("The sum of odd numbers from 1 to {0} = {1}".format(num, odd_total))
You should try replacing the while loop with a for loop like
for i in range(1,num+1):
calculate even and odd sum..
or replace while i==num with either i!=num+1 (i not equal to num) or (i<=num)
The problem is with your test condition of while,
Because, we know, while loop will execute if the condition is true but in your case
while(i == num) :
here i=1 and num=10 the test condition is false and while loop will never execute.
So, replace while i == num: with while i<=num: and it will work fine.
And your last two print statements are wrong(has nothing to do with logic)
print("The sum of even numbers from 1 to {0} = {1}".format(i, even_total))
print("The sum of odd numbers from 1 to {0} = {1}".format(i, odd_total))
It should be
print("The sum of even numbers from 1 to {0} = {1}".format(num, even_total))
print("The sum of odd numbers from 1 to {0} = {1}".format(num, odd_total))
There are few more things but as you are a beginner its enough.

Python if statement using Modulo

I made a list called 'l' then I created a for loop with an if statement inside. The if statement is suppose to check if num is even (equal to 0) if it is then it will print the num if not it will print "Odd number".
Why does the first one print incorrectly ( 2 4 Odd number! )
and the second one prints correctly ( Odd number 2 Odd number 4 Odd number )
I already tried changing the spacing on the first one but I kept getting statement exceptions.
l = [1, 2, 3, 4, 5]
# First
for num in l:
if num % 2 == 0:
print num
else:
print 'Odd number!'
print
print
#Second
for num in l:
if num % 2 == 0:
print num
else:
print 'Odd number!'
Output:
First
2
4
Odd number!
Second
Odd number!
2
Odd number!
4
Odd number!
Indentation. Python uses indentation to figure out scopes in your code, so for your first for loop, it doesn't do anything. Reformat it like this:
for num in l:
if num % 2 == 0:
print num
else:
print 'Odd number!'
The second piece of code was properly indented, that's why it worked.
Python cares about how much whitespace is at the start of a line. This line:
if num % 2 == 0:
is not being considered as part of the for loop.

How to make a list of odd numbers using range and without if statement

I am trying to learn Python with Eric Matthes's Python Crash Course. In one section of "Try it yourself" I have the next task.
Use the third argument of the range() function to make a list of the odd numbers from 1 to 20. Us a for loop to print each number.
I tried:
odd_numbers = []
for value in range(1,11):
number = value % 2 = 1
odd_numbers.append(number)
print(odd_numbers)
This does not work.
Any way I can solve this without an if statement?
for value in range(1,20,2):
print(value)
Do exactly as it says.
The range function takes three arguments: range([start], end, [step])
To get even numbers, start at an even number like 0, and step at 2.
range(0,end,2)
for i in range(1, 11, 2):
print ('This will print only odd numbers:', i)
The output:
This will print only odd numbers: 1
This will print only odd numbers: 3
This will print only odd numbers: 5
This will print only odd numbers: 7
This will print only odd numbers: 9
#this is will create a list of odd numbers 1-20
#create an empty list
odd = []
#create a for loop with range count by 2 & then append to the list
for numbers in range(1, 21, 2):
odd.append(numbers)
print(odd)
#To create a list and only print the odd numbers 1-20
odd = list(range(1,21,2))
for number in odd:
print(number)

Printing prime numbers up to a user's input

I am making a quick program that will ask the user for a number and then output all the prime numbers up to that number:
n=int(input("Enter a number: "))
a=2
if n<=1:
n=int(input("Enter another number: "))
while a<n:
for i in range(2,n):
if a%i==0:
break
else:
print (a)
break
a=a+1
The problem I am having is that it won't print out 2. For example, if I use 20 as my n value, it will print:
Enter a number: 20
3
5
7
9
11
13
15
17
19
You have two mistakes:
The else is incorrectly indented, so it sits with the if not the for (you want to print if all values below a aren't factors of a, not on the first one that isn't); and
Your inner range goes up to n, not a (so always includes a, and a % a == 0).
This will work:
for a in range(2, n):
for i in range(2, a):
if a % i == 0:
break
else:
print(a)
Note I have used a for loop to replace the outer while, which is generally better when you already know where to stop. You can make the code more efficient by checking up to the square root of a, and leaving out even numbers other than 2, but I will leave those optimisations to you.
You can start the loop from 1.
Your loop will start from 1, hence you will get 2 in output.

Categories

Resources