# How to reduce Python code?

Created just such a program and I immediately wondered how it is possible to reduce a number of operators elif or invest in each other, because when I put functions in an if or elif if elif then command execution is simply ignored.

Code:
``````a = -1
b = 1
c = 0
Try = 'Try again...'
run = True
print('How much will 3+4-4*2, 9% and 4 ~-1')
while run:
answer1 = int(input('Enter the first number:'))
answer2 = int(input('Enter the second number:'))
answer3 = int(input('Enter the third number:'))
if answer1 == a & answer2 == b & answer3 == c:
print('Congratulations,you have answered right')
run = False
elif answer1 == a & answer2 == b:
print('You answered true to 1 and 2 question')
print(Try)
elif answer2 == b & answer3 == c:
print('You answered correctly on the 2nd and 3rd question')
print(Try)
elif answer1 == a & answer3 == c:
print('You answered true to 1 and 3 question')
print(Try)
elif answer1 == a:
print('You have answered correctly only on the 1st question')
print(Try)
elif answer2 == b:
print('You have answered correctly only on the 2nd question')
print(Try)
elif answer3 == c:
print('You have answered correctly only on the 3rd question')
print(Try)
else:
print('You said is not true')
print(Try)
print('Good job!')
input('Press ENTER to terminate')``````
March 19th 20 at 08:42
March 19th 20 at 08:44
Solution
``````right_answers = [-1, 1, 0]

print('How much will 3+4-4*2, 9% and 4 ~-1?')
while True:
user_answers = (int(input('Enter {} number:'.format(i))) for i in ('first', 'second', 'third'))
equalities = [a == b for a, b in zip(right_answers, user_answers)]
if all(equalities):
print('Congratulations, you answered correctly!')
break
else:
if sum(equalities):
nums = ', '.join ('{} th'.format(n) for n, i in enumerate(equalities, start=1) if i)
print('You answered correctly in {} question'.format(nums))
else:
print('You said is not true')
print('Try again...')

print('Good job!')
input('Press ENTER to terminate')``````
Very interesting,but will have to figure it all out.Thank you) - Broderick.Keebl commented on March 19th 20 at 08:47
It would be interesting to test these two versions of code in the timeit. - bennie.Schul commented on March 19th 20 at 08:50
A shortened version of harder,still not able to take it apart because some of the variables are taken and do not understand how( - Broderick.Keebl commented on March 19th 20 at 08:53
@Broderick.Keebl, Even it became interesting that there do not understand where does? - deontae_Stros commented on March 19th 20 at 08:56
@Broderick.Keebl, leave this code and a good understanding of the following:
- the full syntax of the While loop
methods str.format() and str.join(),
function zip(), all(), sum(), enumerate(),
generators of lists/tuples. For example, `[i**2 for i in range(5)]` or `[i**2 for i in (1, 2, 3) if i%2 == 1]`.
Then go back to the code, which led Sergey Gornostaev, and the problems with understanding will not be. - bennie.Schul commented on March 19th 20 at 08:59
@bennie.Schul, a While loop and str method.format() I know,and the rest not.Thanks,did not expect such a direct answer that will immediately indicate that you need to study,I am very surprised and grateful to you))

PS what do square brackets? - Broderick.Keebl commented on March 19th 20 at 09:02
@Broderick.Keebl, 123 - bennie.Schul commented on March 19th 20 at 09:05
@bennie.Schul,yeah I understand,it lists - Broderick.Keebl commented on March 19th 20 at 09:08

Find more questions by tags Python