Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Errata]: Chapter 5. Conditionals and variables #13

Open
nafanz opened this issue Nov 10, 2024 · 0 comments
Open

[Errata]: Chapter 5. Conditionals and variables #13

nafanz opened this issue Nov 10, 2024 · 0 comments
Assignees

Comments

@nafanz
Copy link

nafanz commented Nov 10, 2024

Contact Details

No response

What format of the book has this error? You may select more than one option.

Print

What language of the book has this error?

English (EN)

Page Number or Section

Chapter 5, pages 108-109

Detailed description

I have a Russian translation of the book from the publishing house DMK Press, but since it is not among the available options, I chose English.

The book contains the code:

userName = input("What is your name? ")
while userName != "Clark Kent":
    print("You're not superman - try again!")
    userName = input("What is your name? ")
print("You are superman!")

I don't think we should teach people to duplicate code, so I would suggest the following option:

while True:
    userName = input("What is your name? ")
    if userName == "Clark Kent":
        print("You are superman!")
        break
    else:
        print("You're not superman - try again!")

It is more correct and also introduces a new loop break operator.

I would also add an alternative option with match case, which is available since Python 3.10

while True:
    userName = input("What is your name? ")
    match userName:
        case "Clark Kent":
            print("You are superman!")
            break
        case _:
            print("You're not superman - try again!")

Unfortunately, the repository does not contain all the code from the book, otherwise I would have made a correction myself and created a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants