Python Forum
List and function while and else
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List and function while and else
#1
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'
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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,)
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,246 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu

Forum Jump:

User Panel Messages

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