Python Coding Need Assistance - python

I am having trouble with my code, I don't know what I can do. My code has a variable "bheight" and it splits into parts automatically(which I have coded and is good) but I want it so whenever it goes through the "def Building():" code it adds 1 onto bheight[0] so when it loops through next time it'll be bheight[1].
import turtle
turtle.bgcolor("orange")
blist = input('Please enter builidng heights e.g. "50 30 60"')
leo = turtle.Turtle()
bsplit = blist.split()
bheight = list(map(int, bsplit))
size = len(bsplit)
def Water():
leo.penup()
leo.goto(-200,0)
leo.fillcolor('midnightblue')
leo.begin_fill()
for a in range (1,3):
leo.forward(400)
leo.right(90)
leo.forward(150)
leo.right(90)
leo.end_fill()
leo.pendown()
def Building():
for x in range (0,int(size)):
leo.fillcolor('darkslategrey')
leo.begin_fill()
leo.left(90)
leo.forward(bheight[-1+1])
leo.right(90)
leo.forward(20)
leo.right(90)
leo.forward(bheight[-1+1])
leo.left(90)
leo.end_fill()
def Reset():
leo.towards(leo)
Water()
Building()

I agree with #maverick's index fix but don't see any need for the index in the first place. Instead of:
for x in range (0,int(size)):
...
leo.forward(bheight[x])
which really should be:
for x in range(size):
...
leo.forward(bheight[x])
why not simply do:
for height in bheight:
...
leo.forward(height)
The reworked code with this and various other style fixes:
from turtle import Turtle, Screen
def Water():
leo.penup()
leo.setx(-200)
leo.fillcolor('midnightblue')
leo.begin_fill()
for _ in range(2):
leo.forward(400)
leo.right(90)
leo.forward(150)
leo.right(90)
leo.end_fill()
leo.pendown()
def Building():
leo.fillcolor('darkslategrey')
for height in bheight:
leo.begin_fill()
leo.left(90)
leo.forward(height)
leo.right(90)
leo.forward(20)
leo.right(90)
leo.forward(height)
leo.left(90)
leo.end_fill()
blist = input('Please enter building heights e.g. "50 30 60": ')
bsplit = blist.split()
bheight = list(map(int, bsplit))
screen = Screen()
screen.bgcolor("orange")
leo = Turtle()
Water()
Building()
screen.mainloop()

Change the def building code:-
def Building():
for x in range (0,int(size)):
leo.fillcolor('darkslategrey')
leo.begin_fill()
leo.left(90)
leo.forward(bheight[x])
leo.right(90)
leo.forward(20)
leo.right(90)
leo.forward(bheight[x])
leo.left(90)
leo.end_fill()
What was happening in your code was that everytime your where doing -1+1 which will get value at 0th index.Instead just add 'x'.

Related

Python Turtle Issue

I am tryig to create a turtle game where two players can chase around a "goal" shape. I am struggling to get the program to recognize that two shapes are close to each other and therefore that one player should receive a point and then the goal should move. I have pasted my code below, any help would be greatly appreciated.
import turtle as trtl
import random as rand
#initialization / game setup
wn = trtl.Screen()
player1 = trtl.Turtle()
player1.shape('turtle')
player1.fillcolor('red')
player1.penup()
player1.goto(rand.randint(-100,100),rand.randint(-100,100))
player2 = trtl.Turtle()
player2.shape('triangle')
player2.fillcolor('blue')
player2.penup()
player2.goto(rand.randint(-100,100),rand.randint(-100,100))
goal = trtl.Turtle()
goal.shape('circle')
goal.fillcolor('yellow')
goal.penup()
goal.goto(rand.randint(-200,200),rand.randint(-150,-150))
player1_score = 0
player2_score = 0
def player1_up():
player1.setheading(90)
player1.forward(10)
def player2_up():
player2.setheading(90)
player2.forward(10)
def player1_down():
player1.setheading(270)
player1.forward(10)
def player2_down():
player2.setheading(270)
player2.forward(10)
def player1_right():
player1.setheading(0)
player1.forward(10)
def player2_right():
player2.setheading(0)
player2.forward(10)
def player1_left():
player1.setheading(180)
player1.forward(10)
def player2_left():
player2.setheading(180)
player2.forward(10)
if (abs(player1.xcor() - goal.xcor())<5):
if (abs(player1.ycor() - goal.ycor())<5):
player1_score+=1
goal.goto(rand.randint(-200,200),rand.randint(-150,150))
print(player1_score)
#events
wn.onkeypress(player1_up, "w")
wn.onkeypress(player2_up, "i")
wn.onkeypress(player1_down, "s")
wn.onkeypress(player2_down, "k")
wn.onkeypress(player1_right, "d")
wn.onkeypress(player2_right, "l")
wn.onkeypress(player1_left, "a")
wn.onkeypress(player2_left, "j")
wn.listen()
wn.mainloop()
I tried to simplify the code and I have seen this work in other programs. However, I cannot get it to work here!

Seconds Counter and score keeper in with python tkinter

im a beginner in python and i have a big trouble for this code. im making a small match making game and this game made me hate python.
simply, user should match the photos together and try to do it as fast as possible for getting less time.
however, the timer is never working or giving me recursion limit error.
i want a simple second counter and a way to count the score.
so far, this is what i have and the timer is happening only once. so the label just becoming 1
import mycanvas as mc
import tkinter as tk
import random as r
import time
clickedimages=[]
score = 0
seconds=0
def main():
root=tk.Tk()
root.geometry("550x800")
root.resizable(0,0)
coverimg=tk.PhotoImage(file="pics\\qs.png")
name=""
images=[]
coppyimages=[]
allcanvas=[]
lbl=tk.Label(root,text="0",font=('robot',20))
lbl2=tk.Label(root,text=str(score),font=('robot',20))
lbl.place(x=100,y=750)
lbl2.place(x=400,y=750)
#making images
for x in range(1,11):
name="pics\\p"+str(x)+".png"
images.append(tk.PhotoImage(file=name))
coppyimages.append(tk.PhotoImage(file=name))
coppyimages.append(tk.PhotoImage(file=name))
#5x4
#positioning
tx=50
ty=150
counter=1
for x in range(0,5):
for y in range (0,4):
choice=r.randint(0,len(coppyimages)-1)
allcanvas.append(mc.myconvas(root,counter,coppyimages.pop(choice),tx,ty,coverimg))
tx+=120
counter+=1
tx=50
ty+=120
root.after(1000,timer(lbl))
root.mainloop()
def timer(lbl):
global seconds
seconds+=1
lbl.configure(text=str(seconds))
def clicked(event):
if(len(clickedimages)==0):
event.widget.showorhide()
clickedimages.append(event.widget)
elif((len(clickedimages)>0) and (event.widget != clickedimages[0])):
event.widget.showorhide()
clickedimages.append(event.widget)
matchchecker()
def matchchecker():
global clickedimages,score
time.sleep(0.5)
if(clickedimages[0].nameofimage==clickedimages[1].nameofimage):
print("Its a match")
clickedimages[0].place_forget()
clickedimages[1].place_forget()
score+=1
else:
print("wrong :(")
clickedimages[0].showorhide()
clickedimages[1].showorhide()
clickedimages.clear()
gamechecker()
def gamechecker():
global score
if(score==10):
print("Game over")
if __name__ == '__main__':
main()
the class is :
import tkinter as tk
import main
class myconvas(tk.Canvas):
number=0
myimage=None
nameofimage=""
coverimg=None
show=True
def __init__(self, root,n,img,px,py,dfault):
super().__init__()
self["width"] = 100
self["height"] = 100
self.number=n
self.myimage=img
self.maketheimage(dfault,px,py)
self.nameofimage = img.cget('file')
# self.setimagename()
self.coverimg=dfault;
self.bind("<Button-1>",main.clicked)
# root.after(500,self.showorhide())
def maketheimage(self,img,px,py):
self.create_image(50,50,image=img,anchor='center')
self.place(x=px,y=py)
def setimagename(self):
self.nameofimage=self.myimage.cget('file')
def showorhide(self):
if(self.show):
self.create_image(50,50,image=self.myimage,anchor='center')
self.show=False
else:
self.create_image(50,50,image=self.coverimg,anchor='center')
self.show=True
thank you in advance

NameError: name 'height' is not defined on line 10

I'm currently creating a game for a school project on Python. This is what I have so far and Python's telling me NameError: name 'height' is not defined on line 10. What does this mean? I've done lots of research, but I still can't figure it out. Can someone help me?
import turtle
enter code here`import random
def main():
wn=turtle.Screen();
Gameturtle=turtle.Turtle()
Setupuser(Gameturtle,wn)
Setupcontrols(Gameturtle,wn)
Randomanglegenerator(turtle,len,height)
li = []
InsertEnemyturtleintolist(li)
while (True):
Hitchecker(Gameturtle, li)
def Setupuser(myTurtle,window):
window.bgcolor("white")
#width=x height=y
window.setup (width=400, height=400, startx=100, starty=100)
myTurtle.speed(2)
myTurtle.shape('turtle')
myTurtle.penup()
myTurtle.delay(0)
myTurtle.left(90)
myTurtle.mainloop()
window.exitonclick ()
def Hitchecker(myTurtle, enemyTurtles):
for eturtle in enemyTurtles:
distance=myTurtle.distance(eturtle)
if distance <= 1:
print("You lost a life >:^D")
def InsertEnemyturtleintolist(list):
Enemy=turtle.Turtle()
Enemy.shape=("triangle")
Enemy.speed(5)
Enemy.penup()
Enemy.left(90)
Enemy.showturtle()
list.append(Enemy)
def Setupcontrols(turtle, window):
window.onkey(lambda: turtle.goto(turtle.xcor(), turtle.ycor()+15), 'w')
window.onkey(lambda: turtle.goto(turtle.xcor()-15, turtle.ycor()), 'a')
window.onkey(lambda: turtle.goto(turtle.xcor()+15, turtle.ycor()), 'd')
window.onkey(lambda: turtle.goto(turtle.xcor(), turtle.ycor()-15), 's')
window.listen()
def Randomanglegenerator(turtle,len,height):
XYcord = random.randrange(1,5)
print(XYcord)
if XYcord==2 or 4:
Enemy.setheading(90)
main()
There are several problems with your program. First, invalid or questionable Python syntax:
if XYcord==2 or 4:
wn=turtle.Screen();
Should be something like:
if XYcord == 2 or XYcord == 4:
wn = turtle.Screen()
These are equivalent methods and you should use one or the other, not both. And mainloop() is a method of the screen or a function in the package 'turtle', not a method of a turtle instance:
myTurtle.mainloop()
window.exitonclick()
Instead do:
window.mainloop()
or:
window.exitonclick()
Redefining Python built-ins like list is a bad idea:
def InsertEnemyturtleintolist(list):
...
list.append(Enemy)
instead do:
def InsertEnemyturtleintolist(myEnemiesList):
...
myEnemiesList.append(Enemy)
Finally, an infinite loop like this has no place in an event-driven world like turtle:
while (True):
we need to replace it with a timer event. Below is my rewrite of your code to address the above issues and get it to basically function:
from turtle import Screen, Turtle
from random import randrange
def main():
screen = Screen()
turtle = Turtle()
SetupUser(turtle, screen)
SetupControls(turtle, screen)
RandomAngleGenerator(turtle)
enemies = []
for _ in range(5):
InsertEnemyTurtleIntoList(enemies, turtle)
HitChecker(turtle, screen, enemies)
screen.mainloop()
def SetupUser(myTurtle, myScreen):
myScreen.bgcolor('white')
myScreen.setup(width=400, height=400, startx=100, starty=100)
myTurtle.speed('slow')
myTurtle.shape('turtle')
myTurtle.penup()
def SetupControls(myTurtle, myScreen):
myScreen.onkey(lambda: myTurtle.sety(myTurtle.ycor() + 15), 'w')
myScreen.onkey(lambda: myTurtle.setx(myTurtle.xcor() - 15), 'a')
myScreen.onkey(lambda: myTurtle.setx(myTurtle.xcor() + 15), 'd')
myScreen.onkey(lambda: myTurtle.sety(myTurtle.ycor() - 15), 's')
myScreen.listen()
def HitChecker(myTurtle, myScreen, enemyTurtles):
dead = False
for myEnemy in enemyTurtles:
if myTurtle.distance(myEnemy) <= 10:
print("You lost a life >:^D")
dead = True
break
if dead:
myScreen.onkey(None, 'w')
myScreen.onkey(None, 'a')
myScreen.onkey(None, 'd')
myScreen.onkey(None, 's')
else:
myScreen.ontimer(lambda: HitChecker(myTurtle, myScreen, enemyTurtles), 100)
def InsertEnemyTurtleIntoList(array, myTurtle):
enemy = Turtle()
enemy.hideturtle()
enemy.shape('triangle')
enemy.speed('normal')
enemy.penup()
enemy.left(90)
enemy.showturtle()
while enemy.distance(myTurtle) < 30:
enemy.setposition(randrange(-190, 190), randrange(-190, 190))
array.append(enemy)
def RandomAngleGenerator(myTurtle):
turns = randrange(1, 3)
for _ in range(turns):
myTurtle.left(90)
main()
I'm sure it doesn't do everything you want it to do yet, but this should give you a workable starting point to finish your program.

Why does this code give me a NameError when I run it?

I'm trying to code a text based adventure game in Python but I can't get the battle system to work.
This is what I have so far:
import time
import random
battle_chance = 3
enemies = ['A hoard of Goblins', 'A skeleton army', 'A large troll']
random_index = randrange(len(enemies))
def init_battle():
while True:
x = randint(0,5)
time.sleep(2)
if x == battle_chance:
print(enemies[random_index], "Has approached you.\nWhat do you do?")
I always get an error and I can't figure out how to fix it.
Error Thrown: NameError: name 'randrange' is not defined
random_index = randrange(len(enemies))
should be
random_index = random.randrange(len(enemies))
Similarly:
x = randint(0,5)
should be
x = random.randint(0,5)
Alternately, you can also add the below statement in the import line:
from random import randrange, randint
Try replacing these lines:
random_index = random.randrange(len(enemies))
x = random.randint(0, 5)
Simply add at the top of your code the following line
from random import randrange, randint

Python code not working: does not print winner

I was wondering why my code keeps printing 'tan', I can't seem to get it to print the actual winner.
import turtle
import random
turtles = []
def setup():
global turtles
startline = -610
screen = turtle.Screen()
screen.bgpic('pavement.gif')
screen.setup(1290, 720)
turtle_ycor = [-40, -20, 0, 20, 40]
turtle_color = ['pink', 'skyblue', 'indigo', 'turquoise', 'tan']
for i in range(0, len(turtle_ycor)):
new_turtle = turtle.Turtle()
new_turtle.shape('turtle')
new_turtle.penup()
new_turtle.setpos(startline, turtle_ycor[i])
new_turtle.color(turtle_color[i])
new_turtle.pendown()
turtles.append(new_turtle)
def race():
global turtles
winner = False
finishline = 550
while not winner:
for current_turtle in turtles:
move = random.randint(0, 10)
current_turtle.forward(move)
xcor = current_turtle.xcor()
if (xcor >= finishline):
winner = True
current_turtle.forward(0)
turtle.forward(0)
winner_color = current_turtle.color()
print('The winner is', winner_color[1])
setup()
race()
turtle.mainloop()
I tried winner_color[0].
I think I may have found the error in this code.
I found out that with your code the last string value in the list turtle_color always wins.
This is because of this part of your code:
while not winner:
for current_turtle in turtles:
move = random.randint(0, 10)
current_turtle.forward(move)
xcor = current_turtle.xcor() #this should be indented, or itll only run this code
#for the last color in the list, in this case, tan
if (xcor >= finishline): #and all of this code should be indented too
winner = True #so it checks all colors, not just tan
current_turtle.forward(0)
turtle.forward(0)
winner_color = current_turtle.color()
print('The winner is', winner_color[1])
So, the correct code (in full) is:
import turtle
import random
turtles = []
def setup():
global turtles
startline = -610
screen = turtle.Screen()
screen.bgpic('pavement.gif')
screen.setup(1290, 720)
turtle_ycor = [-40, -20, 0, 20, 40]
turtle_color = ['pink', 'skyblue', 'indigo', 'turquoise', 'tan']
for i in range(0, len(turtle_ycor)):
new_turtle = turtle.Turtle()
new_turtle.shape('turtle')
new_turtle.penup()
new_turtle.setpos(startline, turtle_ycor[i])
new_turtle.color(turtle_color[i])
new_turtle.pendown()
turtles.append(new_turtle)
def race():
global turtles
winner = False
finishline = 550
while not winner:
for current_turtle in turtles:
move = random.randint(0, 10)
current_turtle.forward(move)
xcor = current_turtle.xcor()
if (xcor >= finishline):
winner = True
current_turtle.forward(0)
turtle.forward(0)
winner_color = current_turtle.color()
print('The winner is', winner_color[1])
setup()
race()
Tell me if there are still any errors (etc)!
It is possible the winner is in fact 'tan' at the end of every race.
The cause of this can be that random.seed is not called yet. Therefore, the code will initialize with the same seed every time it is run, causing the same sequence of random numbers generated every time having the same winner as result. You can increase randomization by for instance initializing the seed before every call, or only at the top of your code.
Adding this line:
random.seed() # initializes seed using current system time
anywhere in your code should randomize the outcomes.
On the note of reading out the winner_color: I'm assuming
winner_color = current_turtle.color() # gets the color of the current turtle
is now a string containing the color 'tan'. In this case, the index [0] will work on a string, and not a list. See for example
>>> a = 'tan'
>>> a[0]
't'
>>> a[1]
'a'
>>> a[2]
'n'
>>> a[:]
'tan'
>>> a[::-1]
'nat'
Also, have a look at how to present your question in a nice way (the stackoverflow text editor also shows tooltips on styling). This will increase the chances of your question being answered and studied.
Welcome to stackoverflow, I hoped this helps you get your code to work!

Categories