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
#1
Hello! first post here, and it's my first time using Python. I was given a task about creating a console program in Python that allows to accept only 10 integers on a list. Upon entering integers on the list, it will not accept 3 consecutive odd integers. Example, the user entered 4, 8, 5, 3, 6, 1, 3, 5, the eighth integer will not be accepted because it is the third odd integer added on the list consecutively. The user may now continue to enter again on the eighth integer: 8, 3, and 7. The entered integers are: 4, 8, 5, 3, 6, 1, 3, 8, 3 and 7.

I'm still confused cause it still wasn't clarified in our lessons but I was able to make a reference code to be able to count even and odd numbers (I know it's a different thing) but how am I able to prompt an error or an invalid message if the input is an odd number

here's the code I made as a reference:

num1 = int(input("Enter an Integer:"))
num2 = int (input("Enter an Integer:"))
num3 = int (input("Enter an Integer:"))
num4 = int (input("Enter an Integer:"))
num5 = int (input("Enter an Integer:"))
num6 = int (input("Enter an Integer:"))
num7 = int (input("Enter an Integer:"))
num8 = int (input("Enter an Integer:"))
num9 = int (input("Enter an Integer:"))
num10 = int (input("Enter an Integer:"))

even_count, odd_count = 0, 0

for num in num1,num2,num3,num4,num5,num6,num7,num8,num9,num10:

    if num % 2 == 0:
        even_count += 1
    else:
        odd_count += 1

print("Even numbers on the list", even_count)
print("Odd numbers on the list", odd_count)
would be really grateful for any help, thanks!
Reply
#2
Update: I've managed to do some parts of it but I'm still cut off

counter = 1
odd_count = 0
while counter <= 10:
    number = int(input("Enter an Integer:"))
    if number % 2 != 0:
        odd_count +=1
    else:
        odd_count =0

    if odd_count > 3:
        print("3 consecutive odd numbers detected")

    else:
any help would be great really :(
Reply
#3
Hi @gachicardo , you are almost there. You missed one detail:
(Feb-24-2022, 05:15 AM)gachicardo Wrote: to accept only 10 integers on a list
So there must be a list. Define an empty list at the start of your program like "result_list = []".
Then in the "while" loop after you decided the entered number is allowable, append this number to the list AND increase the counter.
Because you increase the counter, the "while" loop will stop in time.

Also note this:
(Feb-24-2022, 05:15 AM)gachicardo Wrote: will not be accepted because it is the third odd integer
So you need to refuse a number if "odd_count == 3". Not "> 3".
gachicardo likes this post
Reply
#4
You should split your program into 2 - 3 parts.
  • A function, which asks the user to input a valid integer. If the user enters something different, he should see an error message and the question should be repeated until the user has entered a valid integer. Users are humans and they fail very often. This is a kind of input validation.
  • A function which uses the previous function 10 times to ask the user for numbers and collect those numbers in a list. This can be done with a for-loop or a list comprehension. Copy&Paste 10 Lines is counterproductive.
  • A function or code block on module level to output the results from the previous function.

One trick to count conditions, which are True, can be done with the sum function.
some_values = [1, 3, 8, 9, 13, 42]

# a generator expression in a function call
even_values = sum(value % 2 == 0 for value in some_values)

# the unrolled version as a for-loop:
results = [] # <- the list contains after the for-loop True and False booleans.
for value in some_values:
    # value % 2 == 0, if this is True, it's an even number
    results.append(value % 2 == 0)

even_values2 = sum(results)

# it translates to:
# sum((False, False, True, False, False, True))
This works because a bool is a subtype of int.
0 == False
1 == True
gachicardo likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Program to check whether a number is palindrome or not PythonBoy 18 2,718 Sep-12-2023, 09:27 AM
Last Post: PythonBoy
  Help needed for a program using loops PythonBoy 5 1,354 Sep-10-2023, 06:51 AM
Last Post: PythonBoy
  Python Program to Find the Factorial of a Number elisahill 2 1,442 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  I will accept any help with this task (compression code) Malin3k 3 2,316 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,931 Mar-02-2020, 10:17 AM
Last Post: buran
  How to collect all integers with minimum number of rounds? meknowsnothing 6 3,287 Jun-11-2019, 08:36 PM
Last Post: jefsummers
  MyProgrammingLab wont accept anything I put in chicks4 2 11,578 Feb-10-2019, 11:44 PM
Last Post: chicks4
  Program that displays the number with the greatest amount of factors ilusmd 3 2,826 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 9,824 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