It's hard for those of us who have been programming for years to "get" what it feels like to start from nothing. I would steer clear of anything needing more than 5 lines of code.
You need to decide the order in which you cover things such as User Input, Output, if, else, while, for, file io etc.
When covering IF do they already know how to get some user input? Print some output? Code a FOR loop? Do arithmentic on integers? Determine whether a number is divisible by another number?
The answers to these questions constrains your examples for IF.
I'd suggest doing Output, Arithmentic, FOR, Divisibility (modulus), User Input before doing IF.
Then I can pose problems such as
Print the first 100 odd numbers
Determine the factors of a number entered by the user
Play a number guessing game (User enters a guess, you print YES or Higher or Lower)
Answer from djna on Stack OverflowHi 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.
if statement - What are good programming questions to exercise the use of "if ... else" in Python? - Stack Overflow
(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
How do you write an if statement in Python?
What is elif in Python?
Can you have an if statement without else?
Videos
It's hard for those of us who have been programming for years to "get" what it feels like to start from nothing. I would steer clear of anything needing more than 5 lines of code.
You need to decide the order in which you cover things such as User Input, Output, if, else, while, for, file io etc.
When covering IF do they already know how to get some user input? Print some output? Code a FOR loop? Do arithmentic on integers? Determine whether a number is divisible by another number?
The answers to these questions constrains your examples for IF.
I'd suggest doing Output, Arithmentic, FOR, Divisibility (modulus), User Input before doing IF.
Then I can pose problems such as
Print the first 100 odd numbers
Determine the factors of a number entered by the user
Play a number guessing game (User enters a guess, you print YES or Higher or Lower)
"Figure out whether a given year is a leap year" springs to mind almost immediately. Just give 'em the rules and turn 'em loose.
Other possibilities (albeit with stuff other than if statements):
- Hunt the Wumpus (you may have to google for this one, I'm showing my age).
- The perennial "detect a win in a Tic Tac Toe (Noughts and Crosses) game" (you could do this with eight
ifstatements if you don't want a loop). - Guessing a number between 1 and 100 as quickly as possible (higher, lower).
For nothing but if/else statements, the leap year one is good. You could also consider:
- Test if a number is a multiple of 3, 5 or 7.
- Given an age, figure out whether someone's a baby, toddler, child, teenager, adult or old codger.
- Calculate grades A-F based on final percentage score.
- Given a number on the roulette table, figure out whether it's red/black, high/low and odd/even.
- Given a blackjack hand, check if it's okay or bust (this is good since J/Q/K morph into 10). You could also figure out whether to draw another card (if total under 17 for example).
That's just a smattering of possibilities that you could get away with.