pythonlistfizzbuzz# I need to optimize a program that goes through numbers

My goal is to create a program that takes numbers from 1 to 100, if a number can be divided by 3, it gives Jack, if divided by 5 Pot, and if a number can be divided by both 3 and 5 it gives JackPot The below code works, but I was told it can be optimized but I do not see how

```
a = 0
while a < 100:
list = []
a = a + 1
if a % 3 == 0:
list.append("Jack")
if a % 5 == 0:
list.append("Pot")
elif a % 3 and a % 5 == 0:
print("JackPot")
if list != []:
print(*list)
else:
print(a)
```

Solution

Try this:

```
for a in range(1, 101):
print(f"{'Jack' if not a % 3 else ''}{'Pot' if not a % 5 else ''}{a if a % 3 and a % 5 else ''}")
```

It uses an f-string to print the Python expressions inside the curly brackets.

`'Jack' if not a % 3 else ''`

prints Jack only if `not a % 3`

, in other words if `a % 3`

is zero. Similarly for `'Pot'`

if `not a % 5`

.

The third set of curly brackets prints `a if a % 3 and a % 5 else ''`

, in other words, prints `a`

if neither `a % 3`

or `a % 5`

is zero.

Output is:

```
1
2
Jack
4
Pot
Jack
7
8
Jack
Pot
11
Jack
13
14
JackPot
16
17
Jack
19
Pot
Jack
22
23
Jack
Pot
26
Jack
28
29
JackPot
31
32
Jack
34
Pot
Jack
37
38
Jack
Pot
41
Jack
43
44
JackPot
46
47
Jack
49
Pot
Jack
52
53
Jack
Pot
56
Jack
58
59
JackPot
61
62
Jack
64
Pot
Jack
67
68
Jack
Pot
71
Jack
73
74
JackPot
76
77
Jack
79
Pot
Jack
82
83
Jack
Pot
86
Jack
88
89
JackPot
91
92
Jack
94
Pot
Jack
97
98
Jack
Pot
```

If you'd like something even shorter, here is a one-liner which does essentially the same thing but with the for-loop iteration inside a list comprehension:

```
[print(f"{'Jack' if not a % 3 else ''}{'Pot' if not a % 5 else ''}{a if a % 3 and a % 5 else ''}") for a in range(1,101)]
```

- Disable silent types conversions in numpy
- How can I import a local module using Databricks asset bundles?
- How to exclude double values in sklearn.metrics.pairwise.euclidean_distances results
- BeautifulSoup can't parse a webpage?
- Printing different values within class in Python
- Pydantic - apply validator on all fields of specific type
- Python program reacting to an event
- Convert date to datetime in Python
- python how to sum amount of a key based on for loop two lists
- Multidimensional indexing in Numpy with tuples as indices for certain axes
- Generating DKIM signatures VIA Python for Custom MTA
- pytorch split array by list of indices
- Creating new columns using for loop based on logical expression in Python
- Python print function stopped working after installing super-gradients in Google Colab
- How to extract the integer from the string '$61.52' to use in the arithmetic oprations in python?
- How can I select the proper openai.api_version?
- What's the use of /256 and %256 in it and how is the address divided into hi and lo bits?
- as_index=False groupBy doesn't work with count
- Python InfiniteTimer Test Code on counting Failed Loop fails
- PyCharm - no tests were found?
- Search results are not showing. And my URL Tab shows: "http://127.0.0.1:8000/search?" instead of this: "http://127.0.0.1:8000/search?q=name"
- Calculating Euler's number
- What does "RuntimeWarning: Enable tracemalloc to get the object allocation traceback" mean? I know how to fix, looking for the meaning
- Numpy, from a list of points, extract ones that have the same range of coordinates without for loop
- multiply and summing same named columns based on name in 2 different DF pandas python
- Reading files from a folder using python
- How to use match case with a class type
- Logging to python file doesn't overwrite file when using the mode='w' argument to FileHandler
- Extract a subset of key-value pairs from dictionary?
- Pandas Resample 2M and 3M for every month