How is it possible to use a while loop to print even numbers 2 through 100?

2024/11/7 10:49:42

I am a beginner and I am stuck on this problem, "Write a python code that uses a while loop to print even numbers from 2 through 100. Hint ConsecutiveEven differ by 2."

Here is what I came up with so far:

 while num in range(22,101,2):print(num)
Answer

Use either for with range(), or use while and explicitly increment the number. For example:

>>> i = 2
>>> while i <=10: # Using while
...    print(i)
...    i += 2
...
2
4
6
8
10>>> for i in range(2, 11, 2): # Using for
...    print(i)
...
2
4
6
8
10
https://en.xdnf.cn/q/120725.html

Related Q&A

Issue with buttons not functioning after start of program

I am new and learning python 3.6 and Ive almost completed my first code project. After doing an exhaustive search to resolve my problem I have not been able to find the answer to what I am sure is a si…

explanation of C implementation pythons len function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

How to compare the attributes start with $ in 2 functions and display match or mismatch

My input file contain attributes if(match($OPTION_EnableDetails, "1") or match($OPTION_EnableDetails_juniper, "1")) {details($juniFileXferStatus,$juniFileXferTimeStamp,$juniFileXfer…

Python comparing elements in two lists

I have two lists:a - dictionary which contains keywords such as ["impeccable", "obvious", "fantastic", "evident"] as elements of the listb - sentences which cont…

Remove all keys that have values of N/A, -, or empty strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…

Sorting algorithms more efficient than bubble sort [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

How can I return the odd numbers of a list, using only recursion in Python? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

TypeError: int object is not iterable; Python 2.7

Here is my code:def numbers_in_lists(string):num = int(string)l = list(num)return lstring = 543987When i run it:print numbers_in_lists(string)I have the following error:l = list(num) TypeError: int obj…

Python: Are `hash` values for built-in numeric types, strings standardised?

I came to this question while pondering about the ordering of set, frozenset and dict. Python doesnt guarantee any ordering, and any ordering is coupled to the hash value at some level. But is the hash…

How to create a sample django project?

This doesnt work for me.$ python django-admin.py startproject myprojectI am running a ubuntu virtual m/c on my windows system. By default ubuntu 12.04 comes with python 2.7.3 so I am using that only I …