简体   繁体   English

跳过Elif条件

[英]Skips Elif Condition

during the execution of the code the program skips all ELIF conditions, going directly to ELSE, even if the ELIF condition is TRUE 在执行代码期间,程序跳过所有ELIF条件,直接进入ELSE,即使ELIF条件为TRUE

a = 0
b = 0
c = 0
r = 0
soma = 1
sub = 2
div = 3
mult = 4
print('enter the number corresponding to the operation you want to do:\n')
print('Sum [1]')
print('Subtraction[2]')
print('Divisao [3]')
print('Multiplication [4]')
r = int(1)
while (r == 1):
    operacao = 0
    operacao = input('\n>')
    if operacao == soma:
            a = int(input('Enter the value of  a:'))
            b = int(input('Enter the value of  b:'))
            c = a + b
            print ('\n A Soma de {} mais {} equivale a: {}'.format(a,b,c))
    elif operacao == sub:
            a = int(input('Enter the value of a:'))
            b = int(input('Enter the value of b:'))
            c = a - b
            print ('\n A subtracao de {} menos {} equivale a: {}'.format(a,b,c))
    elif operacao == div:
            a = int(input('Enter the value of a:'))
            b = int(input('Enter the value of b:'))
            c = a / b
            print ('\n A divisao de {} de {} equivale a: {}'.format(a,b,c))
    elif operacao == mult:
            a = int(input('Enter the value of a:'))
            b = int(input('Enter the value of b:'))
            c = a * b
            print ('\n The multiplication of {} by {} is equivalent to: {}'.format(a,b,c))
    else: #going direct to here...
            print('\n Unrecognized operation')

EXPECTED that the ELIF conditions would work when true,but not working. 预计ELIF条件在真实时会起作用,但不起作用。

input returns a string , so you'll need to do operacao = int(input('\\n>')) , otherwise str == int will always be False : input返回一个string ,所以你需要做operacao = int(input('\\n>')) ,否则str == int将永远为False

x = input("\n>") # I've input 5
x
# '5'

# returns False because x is a string
x == 5
# False

# converts x to int, so returns True
int(x) == 5
# True

# returns True because we are comparing to a string
x == '5'
# True

So for your code: 所以对于你的代码:

# convert the return of input to int for comparing against other ints
operacao = int(input('\n>')) # I'll put 3

if operacao == 1:
    print('got one')
elif operacao == 3:
    print('got three')

# got three

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM