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
#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


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

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