'Replacing specific portions of a Range output with a print statement
I am a newbie to python and I am trying to replace output from a range with results of a specific calculation. In essence, I have a range from 1 to 100 and if the number is divisible by 4, I want my output to say 'Even', and if it's divisible by 5, I want it to say 'Odd' and when it's a value divisible by both, I want it to say 'Both'. I was able to get my script to work, but I'm trying to find out how I get all print statements next to the applicable numerical value or replace the numerical value with the print statement. Here is my code snippet:
while True:
for n in range (1,101):
print(n)
if n%4==0:
print(str(n),'' + 'Even')
if n%5==0:
print(str(n),'' + 'Odd')
if (n%4==0) and (n%5==0):
print (str(n),'' + 'Both')
else:
break
and below is a snippet of my output:
10 Odd
11
12
12 Even
13
14
15
15 Odd
16
16 Even
17
18
19
20
20 Even
20 Odd
20 Both
Any thoughts on how I could go about printing it as below, for example 20 has all three print statements instead of the numerical value because it is divisible by 4, 5 and both, 4 and 5. Thanks
19
Odd, Even, Both
21
22
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
