SoundChaos
SoundChaos

Reputation: 307

Conditional if statements based on items in a list with python 3.x

I am having difficulty getting this assignment to be functional without using anything we haven't "learned" yet...

I have to have a variable defined globally, eg:

x = [80, 70, 65, 60, 50, 40, 45, 40, 35, 7, 0]

given this global variable, I need to call its values with conditional statement to give a particular result. All I can think of based off what we have gone over in class is

x = [80, 70, 65, 60, 50, 40, 45, 40, 35, 7, 0]

if x >= 50:
    print("Good")
else:
    print("Bad")

and this simply will not work... But what I have got to work is the following:

for x in [80, 70, 65, 60, 50, 40, 45, 40, 35, 7, 0]:
    if x >= 50:
        print("Good")
    else:
        print("Bad")

This apparently is just simply not acceptable for the assignment though, due to the global variable is not defined as it should be. Also, the assignment deals with the turtle module, and the characteristics of the turtle will change based on the integer values in the list.

so now i come to answers given here already for an example more directly related to the assignment..

import turtle

def drawBar(t, height):
    t.begin_fill()
    t.left(90)
    t.forward(height)
    t.write('  '+ str(height))
    t.right(90)
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()
    if height >= 50:
        t.fillcolor("blue")
    elif height < 50 and height >= 30:
        t.fillcolor("yellow")
    else:
        t.fillcolor("red")

xs = [80, 70, 65, 60, 50, 40, 45, 40, 70, 7, 0]

maxheight = max(xs)
numbars = len(xs)
border = 10

bob = turtle.Turtle()
bob.color("blue")
bob.pensize(3)

wn = turtle.Screen()
wn.bgcolor("lightgreen")
wn.setworldcoordinates(0-border,0-border,40*numbars+border,maxheight+border)

for a in xs:
    drawBar(bob, a)

wn.exitonclick()

So now that I have it this far, the if statement doesn't accurately reflect the height of the bar for each bar in the program, it seems to give almost random colors, but the colors are always grouped together (which they shouldn't be). I need each bar to be a certain color based on its height, such as above 50 gets blue, less than 50 but equal or more than 30 yellow, everything else red.

Upvotes: 1

Views: 4965

Answers (2)

ATOzTOA
ATOzTOA

Reputation: 35950

You can directly use the list in the for loop, like this:

for y in x:
    if y >= 50:
        print("Good")
    else:
        print("Bad")

Update :

Here is the updated drawBar function. The issue was, you have to set the fillcolor before drawing.

def drawBar(t, height):
    if height >= 50:
        t.fillcolor("blue")
    elif height < 50 and height >= 30:
        t.fillcolor("yellow")
    else:
        t.fillcolor("red")

    t.begin_fill()
    t.left(90)
    t.forward(height)
    t.write('  '+ str(height))
    t.right(90)
    t.forward(40)
    t.right(90)
    t.forward(height)
    t.left(90)
    t.end_fill()

Output:

enter image description here

Upvotes: 2

Daniel
Daniel

Reputation: 19547

for a in x:
    if a >= 50:
        print("Good")
    else:
        print("Bad")

Is this what you are looking for?

Make sure your "a" variable is actually an integer:

>>> str(1)>=5
True

Upvotes: 4

Related Questions