Jump to content

if -elif-else statement not working

- - - - -

  • Please log in to reply
3 replies to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
Good day everyone, I need to print data using if-elif statement. I when I run this code like this, everything went well



    if row['username'] == 'mutago':

        

        print " okay"

        sys.exit(0)

 else:

     print "wrong"

OKAY now i want to include elseif statement to print another data but it returns error. below is the error code


    if row['username'] == 'mutago':


        

        print " okay"

        sys.exit(0)

elif row['password']=='muta55':

print "good"

 else:

     print "wrong"

i have tried also elseif but does not work. ANY HELP PLEASE

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Conditional statements are used something like this:
if username == 'user1' 

  print "Hello user1, now enter your password: " 

  password= gets.chomp 

elsif username == 'user2' 

  print "Oh no, you don't. Your account is banned, spammer; you can't log in. " 

else 

  print "Oh, hello " + username + "; please give us your password, so we can access your data: " 

  password= gets.chomp 

end 


#3
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
okay in this, i need to print error message if ID is greater than 1 and log user in if username is correct and
display error message if the username is wrong.
the code print error message if id is greater than 1 and it authenticates user if correct username is entered
but does no display error message is wrong username is entered rather, it dispalys empty page. any help




if id > '1'

puts 'your id is greater than 1'

# working


elsif username == 'mutago'

puts 'login' 

#working


else 

puts "wrong username"

#here is the problem not working rather dispalys empty page

end



#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Is ID a number? If so, you might want to compare it to 1, instead of '1' . If it's a string, but is meant to be a number, use this code to convert it to a number:
id= id.to_i 
for a whole number or an integer; or:
id= id.to_f 
for a floating point number; if not sure whether it's going to be floating-point or integer, you could use the floating-point version.

As for what the code says, it's something like this:
if id is greater than the string '1' , then do this: { 

  # output the 'your id is greater than 1' message 

} else { 

  if the username is 'mutago' , do this: { 

    # output the 'login' message 

  } else { 

    # output the 'wrong username' message 

  } 

} 

As for why it just displays an empty page, it wouldn't make sense to me, why that happens. Can you say what the variable values are, when the blank page is produced?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users