I keep getting an error telling me that Player is not defined | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I keep getting an error telling me that Player is not defined

I've looked everywhere for the answer to this and I have no idea what the problem is. You are making a video game! The given code declares a Player class, with its attributes and an intro() method. Complete the code to take the name and level from user input, create a Player object with the corresponding values and call the intro() method of that object. Sample Input Tony 12 Sample Output Tony (Level 12) Code: class Player: def __init__(self, name, level): self.name = name self.level = level def intro(self): print(self.name + " (Level " + self.level + ")") Char= Player(name, level) print(Char.name) Char.intro() name=input() level=input() This is the same code as everywhere else with a few things changed. The error keeps telling me that Player is not defined, what am I doing incorrectly?

6th Aug 2021, 4:38 PM
Kevin
8 Answers
+ 9
I've solved it! The solution was very similar to @Lothar but I removed the second print so that it would not print player 1 twice. class Player: def __init__(self, name, level): self.name = name self.level = level def intro(self): print(self.name + " (Level " + self.level + ")") name=input() level=input() Char= Player(name, level) Char.intro()
6th Aug 2021, 5:14 PM
Kevin
+ 6
Kevin Shouldn't the code look like this: https://code.sololearn.com/c3nq2k4TBvqJ/?ref=app
6th Aug 2021, 4:49 PM
Lothar
Lothar - avatar
+ 1
@Lothar that returns Player1 Player1 (Level 4)
6th Aug 2021, 5:02 PM
Kevin
+ 1
@Brain & Bones Syntax error
6th Aug 2021, 5:04 PM
Kevin
+ 1
class Player: def __init__(self, name, level): self.name = name self.level = level def intro(self): print(self.name + " (Level " + self.level + ")") new = Player(input(), input()) new.intro()
25th Aug 2021, 7:40 PM
Leeban J
Leeban J - avatar
+ 1
I have tried this code but it keeps giving me an error
15th Aug 2023, 4:04 PM
Chioma Favour
0
Remember! you can also the following code changed so easy. is enough str() replaced to str(input()). class Player: def __init__(self, name, level): self.name = name self.level = level #was added str() def intro(self): print(self.name + " (Level " + str(self.level) + ")") #was added two line characteristics=Player(input(),input()) characteristics.intro()
28th Nov 2023, 2:24 PM
vahid kamrani
vahid kamrani - avatar
- 1
class Player: def __init__(self, name, level): self.name = name self.level = level def intro(self): print(self.name + f' (Level {self.level})') name = input() level = input() Char = Player(name, level) Char.intro()
31st Dec 2021, 10:58 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar