(beginner) if/elif/else exercises
[BEGINNER COURSE] (If statement exercise went wrong)
It's really hard to tell what is wrong with your code because it isn't formatted properly. Python uses white-space to define and separate individual code blocks and what you've shown us is just a few dozen individual lines of code with no discernible separation between them.
That said, there are a few things that stand out right away:
-
Your input needs to be stored somewhere. The way your code is written, the second this line executes and you respond to it then "POOF" it is gone. Open your terminal then type "python3" to open an interactive terminal. Type "input("What is your gender: ") and see what happens. Now type "gender = input("What is your gender:") What's the difference? Now type "gender" and see what happens.
-
".lower" is a string method that isn't being called (strings are objects that have methods associated with them and methods are like functions that need to be called). Just like when you call your functions at the end of your program with "is_male()" and "gender_print()", ".lower" needs "()" to call this method" Again, in your terminal type "my_string = 'THIS IS MY STRING.'" Now type "my_string.lower" and "my_string.lower()" What's the difference?
-
Learn about scope. There are things happening inside your functions that are independent of what is happening outside of your functions. Variables that are declared inside your function are not necessarily the same as variables that are declared outside of it. If you want to pass attributes of your global scope (like what you captured in your input), then try passing it to your function as input like so "def is_male(gender)" and now you can use this gender in your conditional statement.
-
Again, more scope issues. Lack of indenting makes it unclear, but it would appear that the variable male is being declared inside your variable and thus its scope is limited to inside of that function. If you want to use that variable outside of that function you have to return it to the global scope (end your function with "return male") and store it in a variable in the global scope to use it (try assigning your new function call to a variable with 'male = is_male()' and using this new variable).
I'm sure there are other ways to do these things and other users may have their thoughts or disagree with my opinions, but this is where I would start. Again, I'd like to say that it's a little hard to understand what your code is trying to do when I can't read it as it should be written with the correct formatting so I'm doing a little guess-work. Also, as a beginner, the interactive terminal is your friend. Build your code step-by-step and examine it as you go. Break your code in to manageable pieces that you can test independently so you can better find bugs and understand what your code is doing. When you're sure that each of these manageable pieces work correctly then add them to your ".py" file to run as a complete script.
I hope this helps!
More on reddit.comPractice python coding
if statement with multiple or conditions
How does the structure of an 'if' statement in Python determine the flow of a program?
Discuss how Python's 'elif' statement benefits program flow compared to multiple 'if' statements.
Explain the concept of 'nested if' statements in Python and provide an example scenario where it is useful.
Videos
Hi all.
Can you recommend if/elif/else exercises for beginner level?
I'm studying the "100 days of Python" by Angela Yu and, although I feel like I'm understanding some of the content, I feel like I should study this subject more before moving on with the lessons
Thanks in advance.