I cannot execute if and Elif commands

Asked

Viewed 58 times

-2

I’m taking an IT course and I’m taking my first lessons with Python, I’m using Pycharm but the code I’m doing does not perform the function "if" no matter what I say in response.

print("menu")
reg= input("Deseja registrar seus itens?\n(1)Sim\n(2)Não\nDigite sua resposta: ")
if reg == 1:
    P1 = input("Digite o produto 1: ")
    vp1 = float(input("Digite o valor do produto 1 : "))
    P2 = input("Digite o produto 2: ")
    vp2 = float(input("Digite o valor do produto 2:  "))
    calculo = vp1 + vp2
    print(calculo)
else:
    print("Sinto muito, não podemos te ajudar em mais nada")
  • 1

    If you use the command type(reg) to find out why. It’s related to types, you’re reading a string and is comparing with a integer.

  • my god kkk I’m very Noob yet, where I insert this type(reg) in the code? string I know what it is, integer is the same as int?

  • Please make sure that the content I posted as a response may be useful to the community. If you have any suggestions for improvement just inform in the comments field.

2 answers

1

The problem is that you are reading a value str and is comparing with a value int.

As I commented in the previous answer it is possible to verify the problem using the function type(reg), can be added to your code right after variable declaration reg.

reg= input("Deseja registrar seus itens?\n(1)Sim\n(2)Não\nDigite sua resposta: ")
type(reg)
# <class 'str'>

This way when you do the comparison in the if conditional command, using your code:

if reg == 1:

Assuming you hit the button 1 (only once) and have pressed enter, then it’s like I’m comparing if the str '1' is equal to numeric value 1.

There are some approaches to solving the problem

Convert read value to integer

reg= int(input("Deseja registrar seus itens?\n(1)Sim\n(2)Não\nDigite sua resposta: "))
type(reg)

Check that using this approach an Exception may occur when making the conversion to integer, for example if the user presses the key to an error will occur. Then it is necessary to treat this case.

Here’s an example of how to treat this problem:

try:
    reg= int(input("Deseja registrar seus itens?\n(1)Sim\n(2)Não\nDigite sua resposta: "))
except:
    print('Houve um erro ao ler sua resposta não foi possível transformar em um valor númerico inteiro')

Compare the value read as str

In this case your if condition should be compared with str '1'.

Follow an example:

reg= input("Deseja registrar seus itens?\n(1)Sim\n(2)Não\nDigite sua resposta: ")
if len(reg) > 0 and reg[0] == '1':

This way simplifies a little the way to read the options because it is not necessary to treat the exception of converting to integer values so just make an adjustment to read only the first character of the value read.

To learn more about the command input and if, see the official documentation.

0

The input() function accepts a string as input, a literal.
When in the variable reg the assignment to function, it will compare a string.

no if , is comparing a literal to an integer.
Could use this way if it’s two string.

if reg == 'x':
  ...faça algo

In the current situation of the code, it is comparing an entry of type string with the number 1, so that it compares the option, it makes equal the entry of type float that it made soon below, but using an integer type that way.

reg= int(input("Deseja registrar seus itens?\n(1)Sim\n(2)Não\nDigite sua resposta: "))

informing that it is a whole type.

Then you’re correct about that check:

if reg == 1: 
  ...faça algo

comparing two integers.

print("menu") 
reg= int(input("Deseja registrar seus itens?\n(1)Sim\n(2)Não\nDigite sua resposta: "))# do tipo inteiro
if reg == 1: 
    P1 = input("Digite o produto 1: ")
    vp1 = float(input("Digite o valor do produto 1 : ")) # input do tipo float
    P2 = input("Digite o produto 2: ") # entrada literal
    vp2 = float(input("Digite o valor do produto 2:  "))# entrada do tipo float
    calculo = vp1 + vp2
    print(calculo)
else:
    print("Sinto muito, não podemos te ajudar em mais nada")
  • It is necessary to treat exception when reading an integer.

  • if you type a literal in place of the integer but for simple didactic purposes understand how to deal with the data type is significant to start the studies.https://answall.com/questions/87584/como-converter-uma-vari%C3%A1vel-string-to-int

Browser other questions tagged

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