одна из моих переменных def застряла на том же значении
Я новичок в python и на этом веб-сайте* Мне сложно понять, почему я получаю такое же значение из моей функции Car.name
class Car:
def __init__(self, name, price):
Car.name = name
Car.price = price
Я сохранил данные об автомобилях в классе
Lamborghini_Centenario = Car("Lamborghini Centenario", 2500000)
Rolls_Royce_Sweptail = Car("Roll's-Royce Sweptail", 12800000)
McLarenP1LM = Car("McLaren P1 LM", 3600000)
Koenigsigg = Car("Koenigsigg CCXR Trevita", 4800000)
Lykan_Hypersport = Car("Lykan Hypersport", 3400000)
Lamborghini_Veneno_Roadster = Car("Lamborghini Veneno Roadster", 4500000)
Aston_Martin_Valkyrie = Car("Aston Martin Valkyrie", 3200000)
Bugatti_Chiron = Car("Bugatti Chiron", 2700000)
Pagani_Huayra_BC = Car("Pagani Huayra BC", 2800000)
Ferrari_Sergio = Car("Ferrari Pininfarina\n Sergio", 3000000) <--.
|
#But when I try to get the name of a random car |
#Here is the code I used vvv (Carlist is not shown here) |
#b = random.choice(carlist) | ignore arrow not part of
#b.name | code(sorry couldn't come
#it only gets the name from the bottom of the car data area. --------+ up with a name)
#I have switched it up and have only been getting the bottom value
#it might be due to the turtle module .mainloop() function
вот весь сценарийЗаранее спасибо
import random
import turtle
import os
import time
class Car:
def __init__(self, name, price):
Car.name = name
Car.price = price
higher_true = 0
lower_true = 0
lives1 = 1
score = 0
Lamborghini_Centenario = Car("Lamborghini Centenario", 2500000)
Rolls_Royce_Sweptail = Car("Roll's-Royce Sweptail", 12800000)
McLarenP1LM = Car("McLaren P1 LM", 3600000)
Koenigsigg = Car("Koenigsigg CCXR Trevita", 4800000)
Lykan_Hypersport = Car("Lykan Hypersport", 3400000)
Lamborghini_Veneno_Roadster = Car("Lamborghini Veneno Roadster", 4500000)
Aston_Martin_Valkyrie = Car("Aston Martin Valkyrie", 3200000)
Bugatti_Chiron = Car("Bugatti Chiron", 2700000)
Pagani_Huayra_BC = Car("Pagani Huayra BC", 2800000)
Ferrari_Sergio = Car("Ferrari Pininfarina\n Sergio", 3000000)
carlist = [Lamborghini_Centenario, Rolls_Royce_Sweptail, McLarenP1LM, Koenigsigg, Lykan_Hypersport, Lamborghini_Veneno_Roadster, Aston_Martin_Valkyrie, Bugatti_Chiron, Ferrari_Sergio, Pagani_Huayra_BC]
wn = turtle.Screen()
wn.screensize(1000, 500)
wn.title("Higher or Lower: Car Price Edition")
wn.setup(1050, 650, 0, 0)
Higher = turtle.Turtle()
Higher.penup()
style = ('Courier', 55, 'italic')
Higher.color('black')
Higher.goto(130, -300)
Higher.write('Higher', font=style, align='center')
Higher.hideturtle()
Or = turtle.Turtle()
Or.penup()
Or.color('black')
Or.goto(275, -300)
Or.write('or', font=style, align='center')
Or.hideturtle()
correct = turtle.Turtle()
correct.penup()
correct.color('green')
correct.hideturtle()
Lower = turtle.Turtle()
Lower.penup()
Lower.color('black')
Lower.goto(406, -300)
Lower.write('Lower', font=style, align='center')
Lower.hideturtle()
line = turtle.Turtle()
line.goto(0, 0)
line.shape('square')
line.shapesize(500, 0.5)
line.color('black')
Lives = turtle.Turtle()
Lives.penup()
Lives.color('black')
style = ('Courier', 60, 'italic')
Lives.goto(158, 260)
Lives.write('Lives:', font=style, align='center')
Lives.hideturtle()
Scores = turtle.Turtle()
Scores.penup()
Scores.color('black')
Scores.goto(-258, 260)
Scores.write('Score:', font=style, align='center')
Scores.hideturtle()
wn.addshape(os.path.expanduser("~/Downloads/Lamborghini_Centenario.gif"))
wn.addshape(os.path.expanduser("~/Downloads/RollsRoyceSweptail.gif"))
wn.addshape(os.path.expanduser("~/Downloads/McLaren-P1-LM.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Koenigsegg-CCRX-Trevita.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Lamborghini-Veneno-Roadster.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Aston-martin-Valkyrie.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Bugatti-Chiron.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Pagani-Huayra-BC.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Ferrari-Pininfarina-Sergio.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Lykan-Hypersport.gif"))
BugattiChiron = turtle.Turtle()
BugattiChiron.penup()
BugattiChiron.shape(os.path.expanduser("~/Downloads/Bugatti-Chiron.gif"))
BugattiChiron.goto(-300, 0)
Carname = turtle.Turtle()
Carname.penup()
Carname.color('black')
Carname.goto(-300, -200)
style1 = ('Courier', 40, 'italic')
Carname.write("Bugatti Chiron", font=style1, align='center')
Carname.hideturtle()
def screenclick(x, y):
print(x, y)
if x in range(39, 227) and y in range(-290, -250):
higher_true + 1
wn.listen()
wn.onscreenclick(screenclick)
a = Bugatti_Chiron
b = random.choice(carlist)
bname = turtle.Turtle()
bname.penup()
bname.goto(275, -200)
bname.write(b.name, font=style1, align='center')
# problem ^^^^^^ is here(it's just giving the Car.name of Ferrari_Sergio)
while higher_true == 0:
wn.update
while higher_true == 1:
if b >= a:
score = score + 1
correct.write('Correct!', font=style, align='center')
time.sleep(1.5)
correct.clear()
1 ответ
Решение
Вам нужно установить атрибуты для объекта, а не для класса.
class Car:
def __init__(self, name, price):
self.name = name
self.price = price
Car.name
установит name
атрибут на Car
класс. Вы сможете получить доступ кname
атрибут из Car
class напрямую без использования таких объектов, как Car.name
.
Настройка Car.name
каждый раз обновляет name
атрибут Car
класс, и он обновляется каждый раз, когда вы создаете Car
объект. Следовательно, он сохраняет только последнее значение.