[Tutor] (no subject)

Peter Otten __peter__ at web.de
Mon Jul 27 04:05:35 EDT 2020


sophia lore wrote:

> Create a program that iterates using a *for loop* over all values of
> list_mod. The program must check each value and append the string "True"
> to the list result if the value is even or append the string "False" to
> the list result otherwise.
> In [ ]:
> 
> ### GRADED
> 
> list_mod = list(range(1,15))
> 
> result = []
> 
> 
> 
> 
> I have been working on this question for class for two days. I have tried
> numerous combinations of if/else statements in the for loop to achieve the
> outcome requested. All of the code I have tried returns errors. I can't
> seem to grasp how to set this up. Any help would be appreciated.

If you have a "big" problem that you cannot solve directly it often helps to 
break it into "small" problems that you find easier and solve these "small" 
problems. Then combine the solutions for the "small" problems to solve the 
"big" one.

Think of the question as two problems -- can you write a script that 
iterates over list_mod and appends the string "True" to result for every 
item in list_mod? Write that down.

Then write a script that takes an int

value = int(input("Enter an int value: "))
if ...:  # replace the dots with an expression that checks if value is odd
    print("Value is odd")
else:
    print("Value is even")

Once you have both scripts working and understand what they do you can try 
and combine them into a solution for your original problem. Hints:

- You can drop the line with input()
- Where does the if...else check go (before, into, or after the loop)?
- What has to be done for odd values instead of just printing? For even
  values?

If you get an error message or a result you cannot explain come back here to 
ask. Provide the full traceback and the script that is causing it.



More information about the Tutor mailing list