It is not output after the while statement.

Asked 2 years ago, Updated 2 years ago, 13 views

First of all, I'm sorry that the code is long.

This is the code for the commonly known "Turning the Bookshelf (a game that adds each number of pages that come out after turning the bookshelf once)." Three additional people will turn the book over, and the person with the highest number will get one point The person who gets 10 points in total is the winner, and you want to write a code that ends the game.

But when I ran the code below, it didn't print after the while statement. The error message does not even appear. (It is output only before the while statement.)

Did I type something wrong?

import random

def a_book (n):

    answer_a = 0
    while n>0 :
        answer_a += n%10
        n//=10
    return answer_a  

AP = random.randomrange (100, 999) # Random page of @p = @

print(page spread by f"a is {ap}")

print(the result of f"a is {a_book(ap)}")

def b_book (n):

    answer_b=0
    while n>0 :
        answer_b += n%10
        n//=10
    return answer_b  

bp = random.randrange(100, 999)

print(page spread by f"b is {bp}")

print(the result of f"b is {b_book(bp)}")

def c_book (n):

    answer_c =0
    while n>0 :
        answer_c += n%10
        n//=10
    return answer_c  

cp = random.randrange(100, 999)

print (page spread by f"c is {cp}")

print(the result of f"c is {c_book(cp)}")

a_win, b_win, c_win = 0,0,0

while a_win == 10 or b_win == 10 or c_win == 10:

    if a_book(ap) > b_book(bp) and a_book(ap) > c_book(cp):
        a_win += 1
        print(score of f"a: {a_win}, score of b: {b_win}, score of c: {c_win}")
    elif b_book(bp) > a_book(ap) and b_book(bp) > c_book(cp):
        b_win += 1
        print(score of f"a: {a_win}, score of b: {b_win}, score of c: {c_win}")
    elif c_book(cp) > a_book(ap) and c_book(cp) > b_book(bp):
        c_win += 1
        print(score of f"a: {a_win}, score of b: {b_win}, score of c: {c_win}")
    else:
        print ("Invalid!")

python

2022-09-20 10:26

2 Answers

while a_win < 10 or b_win < 10 or c_win < 10:


2022-09-20 10:26

Let's think about it with strength in our brains!!

# The following function returns True when the winner comes out. Am I right?
def somebody_has_won() :
    return a_win >= 10 or b_win >= 10 or c_win >= 10

# Then the next function returns True when there is no winner.
def nobody_has_one() :
    return somebody_has_one() is False

# But shouldn't you play the game indefinitely when there's no winner? Not when there's a winner.
while (nobody_has_one()) :
    If result_a > result_band result_a > result_c: # Omit below...

# However, the original code is the code that "continues playing the game indefinitely when the winner comes out" when there is no winner. That's why nothing happened.
# There will be many ways to improve, so let's understand the core of the problem and fix it ourselves.
# Bonus 1: How do I define nobody_has_one() without somebody_has_won()?
# Bonus 2: How do I write straight to a statement without nobody_has_one()?


2022-09-20 10:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.