Python Forum
Problem witrh else and elif values.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem witrh else and elif values.
#1
Hello there i am new into this programming world, and yesteday i was trying to apply the else and elif fonction without any success!
I let you a pic down there to illustrate my problem.

on this link you can find easily the screen shot i made for another trade : http://www.developpez.net/forums/d160496...me-python/

Anolibal.
Reply
#2
Bon jour!
I don't know what exactly is the matter with your code, but I tried this and it worked:


a = 150

if (a>5):
   print("bigger")
elif (a < 5):
   print("smaller")
Next time just paste code and error message here, code being within code tags ;)
Reply
#3
Also, you don't need the parenthesis in your 'if' and 'elif' statements:

a = 150

if a > 100:
    print('a is bigger')
elif a < 100:
    print('a is smaller')
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
First of all thanks for your answeres guys !
But even if('i copy paste') you code it still doesn't work ! :( :(
When i type elif or else python reconyze the function but when i type enter
the red message appears agin and again :O That stuff is weird :D
SOS !
Ano

Oh dam yes i found the answere !
For those who are interested :
after the command IF i say print something,
and at this moment i press enter, and then the tiping line goes down.

at this moment i want to type elif but it doesn't work and if i press enter again : >>> will appear again which means that python wait for a new command.
So the thing to do is just to press left arrow once to align the IF , ELIF, and ELSE command so that python understand that THESE are answeres to the IF.

ANOLIBAL
Reply
#5
You're trying to use if/elif interactively.  Which means you need to enter both before the next prompt (">>>").
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> if "spam" == "eggs":
...   print("I doubt it.")
... elif "spam" in ["spam", "spam", "spam", "baked beans", "spam"]:
...   print("More likely.")
...
More likely.
>>>
Reply
#6
as nilamo is describing, you ended the conditional structure before you intended to. place test code in a small file. make it test only one thing. then run it instead of using the interactive mode. long code is hard to work with in interactive because you probably have to type it again a few times. then it is easier to edit the file and run it again.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
   
You hit two times enter, then this block was closed.
In the next lines you tried to continue with your
elif statement, but block was already closed.

If you use the repl (interactive shell), two times ENTER finishes the block.
Just hit only one time enter for the next line.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
if you see "..." it is taking more input for the block. if you see ">>>" you are starting a block.

maybe i need to start a course "Introduction to Python Language Interactive Mode 101" for the Freshmen to get them started.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with my If, Elif, and Else code. ParkerS999 3 936 Sep-03-2020, 01:45 AM
Last Post: bowlofred
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 850 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 865 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Problem with elif statement Haddal99 2 977 May-20-2019, 09:26 AM
Last Post: avorane
  if elif problem :) Runer 2 1,215 Apr-22-2019, 01:57 PM
Last Post: Runer
  problem returning values Naito 7 1,667 Jan-19-2019, 08:21 PM
Last Post: Naito
  Problem with sum of values from .txt file PathhK 2 1,277 Jan-07-2019, 07:40 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020