The problem is in your playerMovement method. You are creating the string name of your room variables (ID1, ID2, ID3):
letsago = "ID" + str(self.dirDesc.values())
However, what you create is just a str. It is not the variable. Plus, I do not think it is doing what you think its doing:
>>>str({'a':1}.values())
'dict_values([1])'
If you REALLY needed to find the variable this way, you could use the eval function:
>>>foo = 'Hello World!'
>>>eval('foo')
'Hello World!'
or the globals function:
class Foo(object):
def __init__(self):
super(Foo, self).__init__()
def test(self, name):
print(globals()[name])
foo = Foo()
bar = 'Hello World!'
foo.text('bar')
However, instead I would strongly recommend you rethink you class(es). Your userInterface class is essentially a Room. It shouldn't handle player movement. This should be within another class, maybe GameManager or something like that.
'Str' object has no attribute error?
python - AttributeError 'str' object has no attribute - Stack Overflow
python - AttributeError: 'str' object has no attribute 'str' - Stack Overflow
AttributeError: 'str' object has no attribute 'items'
Videos
I have some experience with programming in Java, C++, etc. and I am trying to write a simple "To-Do List" program to get used to Python. I'm running into the error: str object has no attribute "completed" when trying to iterate over the list of tasks, check their completion status, and display them.
Here are some relevant pieces of the program:
Constructor for the Task class
def __init__(self, task_name):
self.task_name = task_name
self.completed = False
In the ToDoList class (which holds a list of the task instances created by the user) this is the iteration throwing the error in question:
for idx, task in enumerate(self.tasks, start=1):
status = "Completed" if task.completed else "Incomplete"
print(f"{idx}. {task.task_name} - {status}")
I thought, potentially the problem lies in the fact that the enumerate function is grabbing the string value of the task instance, rather than the object itself, so maybe I can iterate over it the old fashioned way and get around it. So I tried it like this:
counter = 1
for task in self.tasks:
status = "Completed" if task.completed else "Incomplete"
print(f"{counter}. {task.task_name} - {status}")
counter += 1
Yet, it throws the same error. I know there is something I am missing or not understanding correctly here. What is it?
Thanks!
There is something wrong with this function. It shows no errors, but when I try to run it, It says AttributeError: 'str' object has no attribute 'current'. (BTW, i am trying to run it as a flet on spyder)
def leapyears(e):
days_in_month = {1: 31, 3: 31, 4: 30, 5:31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31 }
month = int(EnterMonth_text.value)
year = int(EnterYear_text.value)
if year % 100 == 0:
if year % 400 == 0:
leap_year = True
elif year % 4 == 0:
leap_year = True
else:
leap_year = False
if month == 2 :
if leap_year:
days_in_month[2] = 29
else:
days_in_month[2]= 28
output_textfield.value= days_in_month[month]
page.update()
Your code has multiple problems - not just the one that has been answered by @Tanishq
You entire approach is not ideal. It's much better to limit your Student class to storing data and handling its representation. Acquisition of the data should be dealt with separately.
Something like this:
from typing import Any
class Student:
def __init__(self, name: str):
self._name: str = name
self._data: dict[str, float] = {}
def __add__(self, __value: Any) -> 'Student':
subject, score = __value
self._data[subject] = score
return self
def __str__(self):
r = [self._name]
for subject, score in self._data.items():
r.append(f'{subject} -> {score}')
return '\n'.join(r)
name = input('Student name: ')
student = Student(name)
n = int(input('Number of subjects: '))
for _ in range(n):
subject = input('Subject name: ')
score = float(input('Score: '))
student += (subject, score)
print(student)
Console example:
Student name: John
Number of subjects: 2
Subject name: English
Score: 65
Subject name: Geography
Score: 58
John
English -> 65.0
Geography -> 58.0
Note:
Numeric input validation omitted for brevity
Here is one way of doing what you want
class student(object):
def __init__(self, standard: int, name: str, age: int, address: str):
self.standard = standard
self.name = name
self.age = age
self.address = address
self.calc()
def calc(self):
self.subject = []
self.score = []
N = (int)(input('Please enter number of Subjects: '))
for i in range(N):
item = input('Enter Subject name: ').upper()
self.subject.append(item)
for i in range(len(self.subject)):
item = input(f'Please Enter the score for {self.subject[i]}: ')
self.score.append(item)
student1 = student(standard=10, name="foo", age=13, address="bar")
Your error:
- You are directly calling the
calcfunction of the class,student, without instantiating it. (You can checkout staticmethods in Python if you still want to do this.) - The
selfis then treated as a variable name, and since you pass''as the value of self, it is inferred to be a string. - This is why you get the error, that the
strhas no attributename- because theselfis a string and not the instance.
Some notes:
- You need to instantiate the class,
student, and pass in the required arguments (as shown in the last line of my solution). - You can merge the
printbefore theinputinto one, as shown. - You can merge the
calcfunction with__init__, as mentioned in the comments. - You are returning within the for loop, after you do
self.subject.append(...). This will not let to have more than 1 subject, and your scores will always be empty - since the functioncalcwill never reach the score handling section. - You might want to look at exception handling to handle the input of number of subjects, here.