Python Forum
Program that allows to accept only 10 integers but loops if an odd number was entered
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program that allows to accept only 10 integers but loops if an odd number was entered
#5
It always make sense to have a plan. In order to make plan we must make inventory i.e. what are the terms and conditions.

- accept only 10 integers on a list
- not accept 3 consecutive odd integers

Now we should try to decompose problem to subproblems and subsolutions.

How translate 'only 10 integers on a list' into Python? Every Python list knows it's length i.e. number of elements. So we can do whatever we need until length of the list is less than ten (note <10. Why so? Because on every iteration we add one value to list and we don't want to add additional item to list which has already 10 items)

nums = []
while len(nums) < 10:
    ...
In order to add items to nums and keep tab on consecutive odd numbers we can write:

consecutive = 0

if num % 2:                   # if odd
    consecutive += 1          # add one to counter
    nums.append(num)
else:                         # if not odd i.e. even
    nums.append(num)
    consecutive = 0           # set counter to zero
We count consecutives, but not taking any action based on that. So we should add condition before we append odd number to list (note that condition is 3 <= consecutive: and not ==. Why? Because counter increases before we check it's value, so with consecutively entering odd numbers counter increases and can be more than 3. Counter is reset only if user enters even number)

if 3 <= consecutive:
    print("You can't enter 3 consecutive odd numbers.")
else:
    nums.append(num)
And putting it all together:

nums = []
consecutive = 0

while len(nums) < 10:
    num = int(input('Enter integer: '))
    if num % 2:
        consecutive += 1
        if 3 <= consecutive:
            print("You can't enter 3 consecutive odd integers.")
        else:
            nums.append(num)
    else:
        nums.append(num)
        consecutive = 0
Note that program crashes if user enters any value which can't be converted into integer.
gachicardo likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: Program that allows to accept only 10 integers but loops if an odd number was entered - by perfringo - Feb-24-2022, 10:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Program to check whether a number is palindrome or not PythonBoy 18 2,846 Sep-12-2023, 09:27 AM
Last Post: PythonBoy
  Help needed for a program using loops PythonBoy 5 1,418 Sep-10-2023, 06:51 AM
Last Post: PythonBoy
  Python Program to Find the Factorial of a Number elisahill 2 1,468 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  I will accept any help with this task (compression code) Malin3k 3 2,337 Feb-10-2021, 10:20 AM
Last Post: Larz60+
  How can details be dynamically entered into a list and displayed using a dictionary? Pranav 5 2,951 Mar-02-2020, 10:17 AM
Last Post: buran
  How to collect all integers with minimum number of rounds? meknowsnothing 6 3,329 Jun-11-2019, 08:36 PM
Last Post: jefsummers
  MyProgrammingLab wont accept anything I put in chicks4 2 11,601 Feb-10-2019, 11:44 PM
Last Post: chicks4
  Program that displays the number with the greatest amount of factors ilusmd 3 2,857 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 9,876 Mar-15-2017, 02:46 PM
Last Post: panick1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020