正确颜色和正确位置以及正确颜色和错误位置的策划python问题

gtlvzcf8  于 2021-09-08  发布在  Java
关注(0)|答案(2)|浏览(365)

大家好,下面是我的主脑游戏python代码,现在我有一个问题,例如:当答案是“rgrg”,我输入字母“grrg”,正确颜色和正确位置的输出应该是=2,正确颜色和错误位置的输出应该是=2。但是现在输出是错误的,它显示颜色和正确位置=2,但正确颜色和错误位置=0。所以如果有人知道我的代码哪一部分是错误的?

import random
from random import choice

print("Welcome to Chee Fung Mastermind Games")
opening = input("Hi Do you need instruction or starightly enter into the game. [I/G]\n")
if opening == "I" or opening == "i" :
        print("Computer will automatically generate four colour from list.")
        print("Player must guess 4 colours numbers correct from the list to win the game.")
        print("You have 10 times chances to atempt the game.")
        print("There will be 5 colours in the below list.")
        print("(R)Red, (G)Green, (B)Blue, (P)Purple, (O)Orange")
        print("Player no need to enter the whole word, just need to enter the first alphabetical of the colours.")
        print("Example: for Red colour you just need to enter 'r' or 'R'.")
else:
        print("Let's start the game.")
prev_curt_color = 0
colors = ["R", "G", "B", "Y", "W", "P"]
attempts = 0
game = True

# computer randomly picks four-color code

color_code = []
for i in range(4):
        color_code.append(choice(colors))
print (color_code)

# player guesses the number

while game:
        correct_color = 0
        guessed_color = 0
        player_guess = input("Please enter the four colour:").upper()
        attempts += 1
        # checking if player's input is correct
        if len(player_guess) != len(color_code):
                print ("\nThe color code has exactly four colors. please try again!")
                attempts -= 1
                continue
        for i in range(4):
                if player_guess[i] not in colors:
                        print ("\nLook up what colors you can use in this game.")
                        attempts -= 1
                        break
        # comparison between player's input and secret code and player_guess[i] in color_code
        if correct_color != 4:
                for i in range(4):
                        if player_guess[i] == color_code[i]:
                                correct_color += 1
                        elif player_guess[i] != color_code[i] and player_guess[i] in color_code and correct_color > prev_curt_color:
                                guessed_color += 1
                        prev_curt_color = correct_color
                print("Correct colour and correct place: ", correct_color)
                print("Correct colour and wrong place: ", guessed_color)

        if correct_color == 4:
                if attempts == 1:
                        print ("Wow! You guessed at the first attempt!")
                else:
                        print ("Well done... You needed " + str(attempts) + " attempts to guess.")
                game = False

        if attempts >= 1 and attempts <10 and correct_color != 4:
                print("The attempt time " + str(attempts))
                print("Next attempt: ")
        elif attempts >= 10:
                print ("You didn't guess! The secret color code was: " + str(color_code))
                game = False

        # play or not to play
        while game == False:
                finish_game = input("\nDo you want to play again (Y/N)?").upper()
                attempts = 0
                if finish_game =="N":
                        print ("Thanks for the game! Bye, bye!")
                        break
                elif finish_game == "Y":
                        game = True
                        print ("So, let's play again... Guess the secret code: ")
j0pj023g

j0pj023g1#

我已经检查并调试了你的代码。然后我发现了一个问题当你 color_code = ['R','G','G','G']player_guess ='GGGG' 当程序第一次尝试比较时 correct_color 具有 prev_curt_color . 它们都等于0。所以当你想计算出正确颜色和错误位置的数字时。它将显示0而不是1。下面是已编辑的代码。

if correct_color != 4:
    for i in range(4):
        if player_guess[i] == color_code[i]:
            correct_color += 1
            prev_curt_color = correct_color
        elif player_guess[i] != color_code[i] and player_guess[
                i] in color_code and correct_color >= prev_curt_color:
            guessed_color += 1

顺便说一句,看来你应该学习如何调试python程序这里是一个教程,希望它能帮助调试教程

yvt65v4c

yvt65v4c2#

import random
from random import choice

print("Welcome to Chee Fung Mastermind Games")
opening = input(
    "Hi Do you need instruction or starightly enter into the game. [I/G]\n")
if opening == "I" or opening == "i":
    print("Computer will automatically generate four colour from list.")
    print(
        "Player must guess 4 colours numbers correct from the list to win the game."
    )
    print("You have 10 times chances to atempt the game.")
    print("There will be 5 colours in the below list.")
    print("(R)Red, (G)Green, (B)Blue, (P)Purple, (O)Orange")
    print(
        "Player no need to enter the whole word, just need to enter the first alphabetical of the colours."
    )
    print("Example: for Red colour you just need to enter 'r' or 'R'.")
else:
    print("Let's start the game.")
prev_curt_color = 0
colors = ["R", "G", "B", "Y", "W", "P"]
attempts = 0
game = True

# computer randomly picks four-color code

color_code = []
for i in range(4):
    color_code.append(choice(colors))
print(color_code)

# player guesses the number

while game:
    correct_color = 0
    guessed_color = 0
    player_guess = input("Please enter the four colour:").upper()
    attempts += 1
    color_dict={}
    # checking if player's input is correct
    if len(player_guess) != len(color_code):
        print("\nThe color code has exactly four colors. please try again!")
        attempts -= 1
        continue
    for i in range(4):
        if player_guess[i] not in color_dict: 
            color_dict.update({'{}'.format(player_guess[i]):1})
        else:
            color_dict.update({'{}'.format(player_guess[i]):color_dict.get(player_guess[i]+1)})
        if player_guess[i] not in colors:
            print("\nLook up what colors you can use in this game.")
            attempts -= 1
            break
    # comparison between player's input and secret code and player_guess[i] in color_code
    if correct_color != 4:

        for i in range(4):
            if player_guess[i] == color_code[i] and color_dict.get(player_guess[i]) >= 1:
                correct_color += 1
            elif player_guess[i] != color_code[i] and color_dict.get(player_guess[i]) >1:
                pass
            elif player_guess[i] != color_code[i] and color_dict.get(player_guess[i]) ==1:
                guessed_color += 1
        print("Correct colour and correct place: ", correct_color)
        print("Correct colour and wrong place: ", guessed_color)

    if correct_color == 4:
        if attempts == 1:
            print("Wow! You guessed at the first attempt!")
        else:
            print("Well done... You needed " + str(attempts) +
                  " attempts to guess.")
        game = False

    if attempts >= 1 and attempts < 10 and correct_color != 4:
        print("The attempt time " + str(attempts))
        print("Next attempt: ")
    elif attempts >= 10:
        print("You didn't guess! The secret color code was: " +
              str(color_code))
        game = False

    # play or not to play
    while game == False:
        finish_game = input("\nDo you want to play again (Y/N)?").upper()
        attempts = 0
        if finish_game == "N":
            print("Thanks for the game! Bye, bye!")
            break
        elif finish_game == "Y":
            game = True
            print("So, let's play again... Guess the secret code: ")

相关问题