Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a question from a noob
#1
Dear All

Shouldn't be the ouput of the program is  below ''1''?? Where do I read wrong?

x = 0
my_list = []
while x < 10:
    if x % 2 == 0:
        my_list.append("dog")
    elif x % 3 == 0:
        my_list.remove("dog")
    x = x + 1
print(my_list.count("dog"))
My comment:
0- do nothing
1- do nothing
2-add dog
3-remove dog
4-add dog
5-do nothing
6-add dog, remove dog
7-do nothing
8- add dog
9-remove dog
Reply
#2
Here is what I get:

>>> x = 0
... my_list = []
... while x < 10:
...     if x % 2 == 0:
...         my_list.append("dog")
...     elif x % 3 == 0:
...         my_list.remove("dog")
...     x = x + 1
... print(my_list.count("dog"))
3
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
yes I know that, output is 3. But I want to understand the path which goes '3'. How can be 3 dogs in my_list. I tried all the 'x'variants in the comment section of my question, as a result I get 1. I am misreading something in it.
Reply
#4
Output should be 3, your comment is wrong for x values:
  • 0 - dog is added ( 0 % 2 == 0 )
  • 6 - dog is added, but not removed
if ... elif .. is shortcut for if ... else: if ..., so if first condition is True, second condition is not even evaluated - if statement does not work as "multiple" choices selector. So for 6 dog is only added.

EDIT: if you want to trace my_list changes, put line with print statement on line over your x = x + 1 line - something like print("x = {}, my_list = {}".format(x, my_list))
Reply
#5
Thanks. It helped a lot.
Reply
#6
Test it! Put print() functions to see what is going on.
>>> x = 0
... my_list = []
... while x < 10:
...     print("x = {}".format(x))
...     if x % 2 == 0:
...         my_list.append("dog")
...         try:
...             print(my_list)
...         except:
...             pass    
...     elif x % 3 == 0:
...         my_list.remove("dog")
...         try:
...            print(my_list)
...         except:
...             pass
...     x = x + 1
... print(my_list.count("dog"))
x = 0
['dog']
x = 1
x = 2
['dog', 'dog']
x = 3
['dog']
x = 4
['dog', 'dog']
x = 5
x = 6
['dog', 'dog', 'dog']
x = 7
x = 8
['dog', 'dog', 'dog', 'dog']
x = 9
['dog', 'dog', 'dog']
3
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Function not scriptable: Noob question kaega2 3 1,209 Aug-21-2022, 04:37 PM
Last Post: kaega2
  Noob question about lists adifrank 4 2,918 Nov-19-2020, 03:26 AM
Last Post: adifrank
  Noob question: why is shapesize() not working for my turtle adifrank 8 5,829 Sep-09-2020, 11:13 PM
Last Post: adifrank
  Noob question adifrank 6 2,790 Aug-18-2020, 11:50 PM
Last Post: adifrank
  Noob Question: Sample Data csn113 1 2,199 Feb-18-2019, 06:35 PM
Last Post: micseydel
  Noob question on Mac python3, pydoc3 JamesNJ 0 2,322 Oct-08-2018, 04:26 AM
Last Post: JamesNJ
  Maya Python Noob Question Iurii_Ledin 2 3,313 Jun-08-2018, 09:09 PM
Last Post: Iurii_Ledin
  Noob question about python and Maxplus Strator 0 2,652 May-16-2017, 05:07 PM
Last Post: Strator

Forum Jump:

User Panel Messages

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