Python 369! Help me!

Asked 2 years ago, Updated 2 years ago, 15 views

I am a student who is learning Python. You want to create a 369 game, and each number is entered by the user at every one time. For example, in the beginning, When 'Enter a number:' appears, the user enters 1 and 1 is output Then, if you get a number again, 2, Then, when 'Enter a number:' appears, enter clap to output clap. If there is more than one 3,6,9 in the number that needs to be entered, such as 33,36,.., clap is input/output more than once, such as clapclap. Entering numbers (3,6,9) in the part where 3,6,9 is required will output a game over and end the program. Below is a very simple code that I made based on the above.

while True:

n = input("Enter the next number: ")
if "3" in n or "6" in n or "9" in n:
    print("Game Over!")
    break

else: print(n)

What I'm curious about is

How do I fix/complement to ensure that the code terminates with a game over in the code terminates? So when the game starts and you enter a number, you have to enter the next number properly in the program to continue, and even if the number is wrong or you don't hit as much as you have to clap, you have to print out the game over and escape to the break door.

Example 1)

Enter the next number : 1

1

Enter the next number : 2

2

Enter the next number : 4

Game over!

Example 2)

Enter the next number : 28

28

Enter the next number : clap #29

clap

Enter the next number : clap #30

clap

Enter the next number : clap #31

clap

Enter the next number : clap #32

clap

Enter the next number : clap #33

Game over!

If I didn't have enough explanation, please let me know in the reply. I would appreciate it if you could answer in code form as much as possible.

python

2022-09-21 10:07

2 Answers

Here's what you need to implement:

For 1., you can declare one variable and increase it by 1 each time you receive an input.

For 2., write a function to find the sum of 3, 6, and 9 numbers of the current numbers and compare whether the number of claps entered is the same as the sum obtained above.

def three_six_nine():
    i = 0
    a = ['3', '6', '9']
    while True:
            i += 1
            num = input('Enter your number: ')
            count = (lambda x: sum([x.count(n) for n in a]))(str(i))
            if count:
                    answer = 'clap' * count
                    if num == answer:
                            print(num)
                    else:
                            print('Game over!')
                            break
            else:
                    if i == int(num):
                            print(num)
                    else:
                            print('Game over!')
                            break

Stores the number of times the variable count has 3, 6, and 9 appeared in a specific number.

If this variable is nonzero, compare whether the repeated 'clap' by the size of the variable is equal to the input value.

If 0, compares the current number i with the same value received.

It might seem a bit complicated, but if you study the parts that you're not sure about, you'll understand


2022-09-21 10:07

I think you can decide on the answer you need first, and if the input value is not correct, you can go to the form of outputting game over. Simply put, you can make 369 games internally and check the value

# 369 games
def three_six_nine(n): # If n is 30, prepare answers from 0 to 29
    expected = []
    for i in n:
        clap_count = 0
        For char in str(i): # Convert the number to string and for loop.
            if char in ['3', '6', '9']:
                clap_count += 1
        if clap_count > 0:
            expected.append("clap %d" % clap_count)
        else:
            expected.append(str(i))
    return expected

The 369 game is ready, so if the input value is different from the set answer, you can mark the game over or error.


2022-09-21 10:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.