Reputation: 17
I'm very new to this. I just started programming last week. I need some basic help. My assignment is to input five numbers and get the output to print out "odd" or "even" for each one. This is how I have started:
num = int(input())
if (num % 2) == 0:
print('even')
else:
print('odd')
How can I have five numbers in the input? I don't want to make a hardcoded list; the program has to work with different numbers each time. I hope you understand my question. Thank you for helping out.
EDIT:
I am not supposed to import anything, so I can't use import random
. I'm supposed to input 5 numbers. For example:
input
3
5
2
1
33
Output
Odd
Odd
Even
Odd
Odd
So I have made some progress but it's still wrong.
for _ in range(5):
num = int(input())
if (num % 2) == 0:
print('even')
else:
print('odd')
I now get the output (odd or even) before I have written all numbers in the input. I don't know how to change that. I want to write the five numbers and then get the output. I hope I have explained this well. Sorry for the confusion.
Upvotes: 0
Views: 2146
Reputation: 1
You can use split() function for the input fields you have capturing in variable num.
values = input("Enter space-separated values: ")
num = values.split()
for i in num:
if (int(i) % 2) == 0:
print('even')
else:
print('odd')
You can find the detail about this here
Upvotes: -1
Reputation: 2241
Since you have edited your question, it appears you don't actually want random numbers, but instead you want the user to enter five numbers in a row.
You seem to want the user to enter all five numbers first, and then all the output to be displayed.
In order to accomplish this, first run the input
function five times using a for
loop, and add each input to a list, then use another for
loop to output whether each one is odd or even.
Here is the code:
# Create a list to store the five numbers
nums = []
# Fill the list with five inputs from the user
for _ in range(5):
nums.append(int(input()))
# Explanation of above line:
# - The `append` function adds a new item to a list
# - So this line gets input from the user,
# turns it into an integer,
# and adds it to the list
# Finally, output whether each number is odd or even
# Iterate over the `nums` list using a for loop
for num in nums:
# The code here will run one time for each item in the `nums` list
# Each time the code runs, the variable `num` will store the current item
# So we can do a test on `num` to see if each item is odd or even
if (num % 2) == 0:
print(num, "is even")
else:
print(num, "is odd")
Above is one way to do your task, however it does not follow best practices for the following reasons:
if __name__ == "__main__:"
so that the code only runs when it is supposed toBelow is the code I would probably write if I were given this task myself. It is much longer and perhaps overkill for such a simple task, but you should try to get into good coding habits right from the start.
#!/usr/bin/env python3
"""This script gets five integers from the user and outputs whether each is odd or even"""
def main():
nums = [get_user_num() for _ in range(5)]
for num in nums:
if is_even(num):
print(num, "is even")
else:
print(num, "is odd")
def get_user_num():
num = input("Enter an integer: ")
while True:
# Repeat until the user enters a valid integer
try:
return int(num)
except ValueError:
num = input("That was not an integer - please try again: ")
def is_even(num):
return (num % 2) == 0
if __name__ == "__main__":
main()
Explanations of some of the things in the improved code:
if __name__ == "__main__":
:Upvotes: 0
Reputation: 80
Try this:
nums = input('Enter your numbers: ').split()
for num in nums:
if int(num) % 2 == 0:
print('even')
else:
print('odd')
Upvotes: 1
Reputation: 141
Please just learn something new about the list comprehension:
ls = [(print(num, "Even") if (num % 2) == 0 else print(num, "Odd")) for num in range(1,6)]
Output:
1 Odd
2 Even
3 Odd
4 Even
5 Odd
Upvotes: 0
Reputation: 1245
A bit on the elaborative side but since you are learning to program, you should be gather various problem specifications and then design you solution
import random
experiment_to_run = int(input()) # how many times you want to run the experiment, let us say default is 5, but that can be user input as well
lower_bound_of_numbers = int(input()) # lower bound of the integer range
upper_bound_of_numbers = int(input()) # upper bound of the integer range
def print_even_or_odd(experiment_to_run = 5, lower_bound_of_numbers = 1, upper_bound_of_numbers = 10):
for current_experiment_run in range(experiment_to_run):
current_number = random.randint(lower_bound_of_numbers, upper_bound_of_numbers) # this generates random integer between lower_bound_of_numbers (inclusive) and upper_bound_of_numbers (exclusive)
if current_number % 2 == 0:
print ('even')
else:
print ('odd')
# dry run
print_even_or_odd(experiment_to_run, lower_bound_of_numbers, upper_bound_of_numbers) # feel to add current_number also to the log if needed for debugging purpose
Upvotes: 0
Reputation: 359
Hi👋🏻 Hope you are doing well!
If I understood your question correctly, you are trying to achieve something similar to this:
import random
# you can define your own limits
# or you can use numpy to generate random numbers from the different distributions
number = random.randint(0, 999)
print(f"Current number: {number}.")
print("Even!") if number % 2 == 0 else print("Odd!")
Upvotes: 1