Python Forum
List and function while and else - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List and function while and else (/thread-21651.html)



List and function while and else - doug2019 - Oct-08-2019

Hi! I need code a list "d" to plot the s x t graph. The list "d" must have six itens too but if user input a value greater "a" in the list this value must be 0 in according to the equation "d=x*0". This code is a simplified example. Can someone help me?

import matplotlib.pyplot
a=int(input("a=")
b=int(input("b=")
c=[0,1,2,3,4,5]
d=[]
x=0
while (x<=a):
    d=b*(a-x)
    x=x+50
else:
    d=x*0
    d.append(x) 
matplotlib.pyplot.title("Average Speed")
matplotlib.pyplot.plot(c,d)
matplotlib.pyplot.xlabel("t [s]")
matplotlib.pyplot.ylabel("s [mi]")
matplotlib.pyplot.show()   
Note
When I run the code it displays the following error message:
AttributeError: 'int' object has no attribute 'append'


RE: List and function while and else - ichabod801 - Oct-08-2019

I think if you had more descriptive variable names you might figure this out for yourself. You initialize d to a list on line 4, then change it to an integer on lines 7 and 10, and then try to append to it on line 11. It seems like you are mixing d up with something else when you assign integers to it.

The else clause is unclear. You have no break statement, so it will always trigger. But it will only run after the while loop is finished, so even if the other problems are fixed, d will only ever have one item in it.


RE: List and function while and else - doug2019 - Oct-08-2019

(Oct-08-2019, 06:27 PM)ichabod801 Wrote: I think if you had more descriptive variable names you might figure this out for yourself. You initialize d to a list on line 4, then change it to an integer on lines 7 and 10, and then try to append to it on line 11. It seems like you are mixing d up with something else when you assign integers to it. The else clause is unclear. You have no break statement, so it will always trigger. But it will only run after the while loop is finished, so even if the other problems are fixed, d will only ever have one item in it.

I'm coded break. The graph is now being generated, but another error message has displayed.

a=int(input("a="))
b=int(input("b="))
c=[0,1,2,3,4,5]
d=[]
x=0
while (x<=a):
    x=x+50
    d=b*(a-x)
    break
else:
    x=0
d.append(x) 
ValueError: x and y must have same first dimension, but have shapes (6,) and (1,)


RE: List and function while and else - ichabod801 - Oct-08-2019

Just throwing a random break in doesn't fix the problem. Now your loop only executes once, and the else clause never activates.

In the future, please give the full text of all error messages, so I can see where the problem is. If it's on the pyplot.plot statement, it's still going to be a problem after you fix the loop. Your list c is fixed length, while (if your loop is working) d's length depends on user input.

And you are still assigning integers to d.