a = int(input('Введите первое число: ')) b = int(input('Введите второе число: ')) c = int(input('Введите третье число: ')) d = int(input('Введите четвертое число: ')) if a > b and a > c and a > d: a = r elif b > a and b > c and b > d: b = r elif c > a and c > b and c > d: c = r else: d = r P = a * b * c * d X = P / r print ('P: '(P)) print ('X: '(X)) Closed due to the fact that off-topic participants Sergey Gornostaev , Vladimir Martyanov , Kromster , jfs , 0xdb Apr 7 '18 at 21:10 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Vladimir Martyanov, Kromster, jfs, 0xdb
- 2Why do you think something is wrong in the code? - Scarabyte pm
- After entering the data, an error occurs - Arseniy
- oneWhat error occurs supplement the question - Twiss
- Possible duplicate issue: SyntaxError: multiple statements found while compiling a single statement - Sergey Gornostaev
5 answers
I do not know what is wrong with you, but the assignments that I see:
a = r
b = r
c = r
d = r
and operation
X = P / r
are executed BEFORE, as the variable r takes some value. This error is not only in Python. This is a bug in any programming language.
- Can you tell me how it should be? - Arseniy
- I do not know what you want to do. Can set r = 10000 first. Can write r = a, r = b, etc. This is semantics, not syntax. I described your mistake. And how to rule - your task. - passant
In the block of each condition, instead of:
a = r write
r = a Never forget that the variable to the left is assigned the value of the expression to the right. And explain in questions what is wrong with your program more understandable.
a = int(input('Введите первое число: ')) b = int(input('Введите второе число: ')) c = int(input('Введите третье число: ')) d = int(input('Введите четвертое число: ')) if a > b and a > c and a > d: r = a elif b > a and b > c and b > d: r = b elif c > a and c > b and c > d: r = c else: r = d P = a * b * c * d X = P / r print ('P: ', P) print ('X: ', X) Try this
- And how will your algorithm work if all four numbers entered are the same? Or even at least two of them? - passant
If we refactor, so refactor
from functools import reduce from operator import mul names = ('первое', 'второе', 'третье', 'четвертое') vars = [int(input('Введите {} число: '.format(n))) for n in names] P = reduce(mul, vars, 1) X = P/max(vars) print('P: ', P) print('X: ', X) Python reads code like a person: from left to right, top to bottom. So when you
if a > b and a > c and a > d: a = r write a = r, then Python reads this as: "assign the value of the variable r to the variable a", not vice versa! And, because The variable r has not been declared before, a NameError exception is raised. Therefore, @Nikita Vavilov corrected your code correctly.