重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

電子書籍の厳選無料作品が豊富!

python初心者です。継承について、ある書籍で勉強しています。OSはWindows10です。

次のファイルを実行したいです。

i_student_card_test.py
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
from i_student_card import IStudentCard

a = IStudentCard(2345, 'John Smith', 'イギリス')

print(f'a.id:{a.id}')
print(f'a.name:{a.name}')
print(f'a.nationality:{a.nationality}')
a.print_info()
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

関連するファイルは、次の2つです。

i_student_card.py
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
from student_card import StudendtCard

class IStudentCard(StudentCard):
def __init__(self, id, name, nationality):
self.nationality = nationality
super().__init__(id, name)
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

student_card.py
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
class StudentCard:
def __init__(self, id, name):
self.id = id
self.name = name

def print_info(self):
print('学籍番号:', self.id)
print('氏名:', self.name)
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

これら3つのファイルを同じフォルダに置いて、Windows PowerShellの上で、

python i_student_card_test.py

と打ち込んでみると、

Traceback (most recent call last):
File "D:\test\i_student_card_test.py", line 1, in <module>
from i_student_card import IStudentCard
File "D:\test\i_student_card.py", line 1, in <module>
from student_card import StudendtCard
ImportError: cannot import name 'StudendtCard' from 'student_card' (D:\test\student_card.py)

と表示されて実行されませんでした。
(フォルダ名はD:\testに書き換えました。)

本には、

▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
a.id:2345
a.name:John Smith
a.nationality:イギリス
学籍番号: 2345
氏名: John Smith
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

というのが正しい実行結果だとあります。

なぜうまくいかないのでしょうか。よろしくお願いいたします。

A 回答 (1件)

> なぜうまくいかないのでしょうか。

よろしくお願いいたします。

ちったぁ落ち着けよ、って話だなぁ。エラーを良く読む。

> ImportError: cannot import name 'StudendtCard' from 'student_card' (D:\test\student_card.py)

ImportError: cannot import name 'StudendtCard'
                    ^

スペルが間違ってるだろ。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。そうですね、落ち着いて、自分で探すようにします。

お礼日時:2021/08/31 12:20

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!