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.
If you use the command
type(reg)
to find out why. It’s related to types, you’re reading astring
and is comparing with ainteger
.– Danizavtz
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?
– Hera
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.
– Danizavtz