Python conditional deviation

Asked

Viewed 267 times

0

I made this code, but it does not compile. I am using Visual Studio 2015.

# Exercicio Python 2.6
import utf8

#PROGRAMA TESTE DOS IFS

#Usando Coding para assento nas Strings

input ("Digite valor de A ")
input ("Digite valor de B ")
input ("Digite valor de C ")
#TESTE
if (a > b):
    if (a > c):
         print("valor maior que A  " .a)
    else:
         print("valor Menor que A  " .c)
         # Else sempre tem que ter os : como assentuacaoo final
else:
    if (b > c):
    print("O maior valor B  ".b)
else
    print("O maior valor C  ".c)
  • 1

    And what is the problem presented? You can see right away that it doesn’t make any sense.

  • You have the code with the very confusing indentations and they give rise to misunderstandings. Moreover the concatenations are not correct, not with point they are made in python

  • I suggest you describe your problem better. I denied the question, because I can’t understand what the real problem is behind it.

1 answer

6

Syntax

Let’s first start with the syntax errors.

import utf8

input ("Digite valor de A ")
input ("Digite valor de B ")
input ("Digite valor de C ")

if (a > b):
    if (a > c):
         print("valor maior que A  " .a)  #(1)
    else:
         print("valor Menor que A  " .c)  #(2)
else:
    if (b > c):
    print("O maior valor B  ".b)          #(3)
else                                      #(4)
    print("O maior valor C  ".c)          #(5)
  1. Python allows you to use any number of whitespace for indentation, as long as it is constant throughout your code. That is, if in one location you use 4 spaces (recommended by PEP 8), all indentations in the code must be 4 spaces. In #(1) and #(2) indentation has 5 spaces, which will generate error.

  2. The Python language uses indentation as logical block delineators. That is, while the C language, for example, uses characters {} to define what is inside a conditional block, Python uses indentation, so in #(3), #(4) and #(5), an indentation of 4 spaces will be required. Also, in #(4) lacked a : after the else.

Semantics

Now to semantic errors (that is, the code is completely formatted according to Python standards, but will not work for logical errors).

import utf8                               #(6)

input ("Digite valor de A ")              #(7)
input ("Digite valor de B ")              #(8)
input ("Digite valor de C ")              #(9)

if (a > b):
    if (a > c):
        print("valor maior que A  " .a)   #(10)
    else:
        print("valor Menor que A  " .c)   #(11)
else:
    if (b > c):
        print("O maior valor B  ".b)      #(12)
    else:
        print("O maior valor C  ".c)      #(13)
  1. The library utf8 in #(6) is not native to Python - and I, in particular, have never heard of one by that name. If it is really a library, make sure it is properly installed and in the same version of Python that you are running. If the intention is only to declare the encoding used in the code, use the header # -*- coding: utf-8 -*-. Read more on PEP 263.

  2. In #(7), #(8) and #(9) you use the function input to ask the user values of A, B and C. Problems:

    4.1. Even if the user enters the three values, they are not stored anywhere. You should, if you want to use the given value, store it in a variable.

    4.2. Use of the function is not recommended input in Python versions 2.6 and 2.7. Best to use raw_input and convert the value to integer after: int(raw_input("...")).

  3. In #(10), #(11), #(12) and #(13) you perform string concatenation with the character .. I believe this is the standard of the PHP language. In Python, the operator is used + or format the string. If you concatenate, take care of the variable type, as it will not be possible to concatenate a string with an integer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.