亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果單詞中有字符則只執行一次操作

如果單詞中有字符則只執行一次操作

交互式愛情 2023-10-25 10:30:50
我創建了一個 python 劊子手游戲,除了一個與計算失敗嘗試次數相關的邏輯錯誤外,它運行良好。    failed = 0    for char in word:        if char in letter_guess:            print(char, end="")            if letter_guess == word:                print("")                print('CONGRATULATIONS! YOU WON')                sys.exit()        else:            failed = failed + 1            print('_ ', end="")            if failed == 15:                print("GAME OVER! your word was", word)                sys.exit() 當玩家猜錯一個字母時,它不是只添加一個,而是為每個不是玩家猜到的字母的字母添加一個。例如,如果單詞是“star”,而玩家猜到了字母“e”,那么程序將添加 4 表示失??;每個字母一個。我不知道如何解決這個問題,以便它只執行一次這個特定的功能,因為我仍然需要它為每個錯誤的字母添加一個“_”。我想也許我可以將 4 個字母單詞的嘗試次數乘以 4,但我不確定這是否有效。這是完整的代碼import randomimport timeimport sys# GAME INTRODUCTIONprint('HANGMAN')name = input('Username: ')print('Welcome', name, 'are you ready to play?')answer = ''print("type 'start' to begin")while answer != 'start':    time.sleep(0.7)    answer = input()print("OK! Let's begin")print("pick the mode/difficulty of the game")time.sleep(0.7)print("1-Easy")print("2-Medium")print("3-Difficult")# POSSIBLE WORDS IN GAMEwords_4 = ['time', 'king', 'song', 'disk', 'meal',           'cell', 'hair', 'menu', 'math']words_5 = ['world', 'paper', 'hotel', 'queen', 'uncle',           'night', 'hotel', 'shirt', 'pizza']words_6 = ['person', 'tennis', 'camera', 'sector',           'potato', 'safety', 'growth', 'thanks']# CHOOSING GAME DIFFICULTYmode = str(input())if mode in ['Easy', '1', 'easy']:    word_list = words_4    letter_num = 4    print("your word consists of 4 letters")elif mode in ['Medium', '2', 'medium']:    word_list = words_5    letter_num = 5    print("your word consists of 5 letters")elif mode in ['Difficult', '3', 'difficult']:    word_list = words_6    letter_num = 6    print("your word consists of 6 letters")else:    print("Mode does not exist!")
查看完整描述

3 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

您的代碼中有幾個問題。

  1. 玩家永遠不會獲勝,因為letter_guess == word只有當該單詞只有一個字母或用戶每輪能夠輸入多個字母時才為真,我不認為這是這里的想法。

  2. 在失敗中,您不是在計算嘗試次數,而是在計算丟失的字母。換句話說,您在每次嘗試中都在計算“_”。

我將在這里為您留下一些固定的代碼,以便您可以將其用作參考:

我創建了一個 python 劊子手游戲,除了一個與計算失敗嘗試次數相關的邏輯錯誤外,它運行良好。


    failed = 0

    for char in word:

        if char in letter_guess:

            print(char, end="")

            if letter_guess == word:

                print("")

                print('CONGRATULATIONS! YOU WON')

                sys.exit()

        else:

            failed = failed + 1

            print('_ ', end="")

            if failed == 15:

                print("GAME OVER! your word was", word)

                sys.exit() 

當玩家猜錯一個字母時,它不是只添加一個,而是為每個不是玩家猜到的字母的字母添加一個。例如,如果單詞是“star”,而玩家猜到了字母“e”,那么程序將添加 4 表示失??;每個字母一個。我不知道如何解決這個問題,以便它只執行一次這個特定的功能,因為我仍然需要它為每個錯誤的字母添加一個“_”。我想也許我可以將 4 個字母單詞的嘗試次數乘以 4,但我不確定這是否有效。


這是完整的代碼


import random

import time

import sys


# GAME INTRODUCTION

print('HANGMAN')

name = input('Username: ')

print('Welcome', name, 'are you ready to play?')

answer = ''

print("type 'start' to begin")

while answer != 'start':

    time.sleep(0.7)

    answer = input()

print("OK! Let's begin")

print("pick the mode/difficulty of the game")

time.sleep(0.7)

print("1-Easy")

print("2-Medium")

print("3-Difficult")


# POSSIBLE WORDS IN GAME

words_4 = ['time', 'king', 'song', 'disk', 'meal',

           'cell', 'hair', 'menu', 'math']

words_5 = ['world', 'paper', 'hotel', 'queen', 'uncle',

           'night', 'hotel', 'shirt', 'pizza']

words_6 = ['person', 'tennis', 'camera', 'sector',

           'potato', 'safety', 'growth', 'thanks']



# CHOOSING GAME DIFFICULTY

mode = str(input())


if mode in ['Easy', '1', 'easy']:

    word_list = words_4

    letter_num = 4

    print("your word consists of 4 letters")

elif mode in ['Medium', '2', 'medium']:

    word_list = words_5

    letter_num = 5

    print("your word consists of 5 letters")

elif mode in ['Difficult', '3', 'difficult']:

    word_list = words_6

    letter_num = 6

    print("your word consists of 6 letters")

else:

    print("Mode does not exist!")


number = 0

i = 0

dash = ('_ ')

word = random.choice(word_list)

for char in word:

    i = i + 1

for number in range(i):

    print(dash, end="")

failed = 0

guessed = ('')

print("")


#CHECKING CHARACTER PLAYER INPUTTED

tries = True

while tries is True:

    print("")

    print("")

    letter_guess = input('Guess any letter: ')

    for char in word:

        if char in letter_guess:

            print(char, end="")

            if letter_guess == word:

                print("")

                print('CONGRATULATIONS! YOU WON')

                sys.exit()

        else:

            

            print('_ ', end="")

            if failed == 15:

                print("GAME OVER! your word was", word)

                sys.exit()

Pythonpython-3.x


查看完整回答
反對 回復 2023-10-25
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

我對您的代碼做了一些小更改,而不是增加失敗的循環,您必須將其放在循環之外for,但仍然在while循環內部。


并且您需要將其放在循環sys.exit()外部while,因為您使用,如果字母猜測全部正確,則while Tries is True可以將其放在循環Tries = False內部,如果 , 則可以將 1 放在循環外部forforfailure = 15


希望這是你想要達到的目標


tries = True

while tries is True:

    print("")

    print("")

    letter_guess = input('Guess any letter: ')

    is_failed = False

    for char in word:

        if char in letter_guess:

            print(char, end="")

            if letter_guess == word:

                print("")

                print('CONGRATULATIONS! YOU WON')

                tries = False

                break

        else:

            is_failed = True

            print('_ ', end="")


    if is_failed:

        failed = failed + 1

        is_failed = False


    if failed == 15:

        print('')

        print('')

        print("GAME OVER! your word was", word)

        tries = False


sys.exit()

如果您想存儲以前的答案,并使您的代碼更簡潔:


tries = True

guessed_word = ['_' for _ in range(len(word))]

while tries is True:

    print("")

    print("")

    letter_guess = input('Guess any letter: ')


    is_failed = False


    for i, char in enumerate(word):

        if char in letter_guess:

            guessed_word[i] = char

        else:

            is_failed = True


    print(' '.join(guessed_word))


    if is_failed:

        failed = failed + 1

        is_failed = False


    if failed == 15:

        print('')

        print("GAME OVER! your word was", word)

        tries = False


    if '_' not in guessed_word:

        print("")

        print('CONGRATULATIONS! YOU WON')

        tries = False


sys.exit()


查看完整回答
反對 回復 2023-10-25
?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

我對您的代碼做了一些小更改,而不是增加失敗的循環,您必須將其放在循環之外for,但仍然在while循環內部。


并且您需要將其放在循環sys.exit()外部while,因為您使用,如果字母猜測全部正確,則while Tries is True可以將其放在循環Tries = False內部,如果 , 則可以將 1 放在循環外部forforfailure = 15


希望這是你想要達到的目標


tries = True

while tries is True:

    print("")

    print("")

    letter_guess = input('Guess any letter: ')

    is_failed = False

    for char in word:

        if char in letter_guess:

            print(char, end="")

            if letter_guess == word:

                print("")

                print('CONGRATULATIONS! YOU WON')

                tries = False

                break

        else:

            is_failed = True

            print('_ ', end="")


    if is_failed:

        failed = failed + 1

        is_failed = False


    if failed == 15:

        print('')

        print('')

        print("GAME OVER! your word was", word)

        tries = False


sys.exit()

如果您想存儲以前的答案,并使您的代碼更簡潔:


tries = True

guessed_word = ['_' for _ in range(len(word))]

while tries is True:

    print("")

    print("")

    letter_guess = input('Guess any letter: ')


    is_failed = False


    for i, char in enumerate(word):

        if char in letter_guess:

            guessed_word[i] = char

        else:

            is_failed = True


    print(' '.join(guessed_word))


    if is_failed:

        failed = failed + 1

        is_failed = False


    if failed == 15:

        print('')

        print("GAME OVER! your word was", word)

        tries = False


    if '_' not in guessed_word:

        print("")

        print('CONGRATULATIONS! YOU WON')

        tries = False


sys.exit()


查看完整回答
反對 回復 2023-10-25
  • 3 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號