While循环 – len(Words)循环六次,“flower”有六个字母。

huangapple go评论65阅读模式
英文:

While loop -- Count of len(Words) loops six time of the "flower" has six Letter

问题

以下是您提供的代码的翻译部分:

当count小于单词长度len(word)时循环六次因为单词"Flower"有六个字母然后我需要询问用户再次猜测如果猜测不正确再次运行同一循环然后再次要求输入直到输入最终正确我该如何实现这一目标再次强调我的目标运行六次循环要求输入不正确的输入会重新开始该过程...

word = 'flower'
print()
print('提示:')
count = 0
while count < len(word):
    print('_', end='')
    count += 1
print()
print()
attempts = 0 
guess = input('你猜是什么词?')
count = 0
while len(guess) != len(word):
    guess = input('请输入正确长度的单词:')
    attempts += 1

while count < len(guess):
    if guess[count].lower() not in word:
        print('_', end='')
    elif guess[count].lower() == word[count]:
        print(guess[count].upper(), end='')
    else:
        print(guess[count].lower(), end='')
        count += 1
    attempts += 1
print()
print(f'你猜了{attempts}次。')
print()

请注意,我已经根据您的要求仅提供了代码的翻译部分,没有包括其他内容。如果您需要进一步的帮助,请告诉我。

英文:

While count<len(word) loops six times because the word "Flower" has six letters. i then need to ask the user to guess again, and if the guess is incorrect , run that same loop again, and then ask for input again until the input is finally correct. how can i accomplish this? again, my goal: Run loop six time ask for input. incorrect input restarts process...

word = &#39;flower&#39;
print()
print(&#39;Hint : &#39;)
count = 0
while count&lt; len(word):
    print(&#39;_&#39;, end=&#39;&#39;)
    count += 1
print()
print()
attempts = 0 
guess = input(&#39; what is your guess? &#39;)
count=0
while len (guess) != len(word):
    guess = input(&#39;Please enter correct length of word : &#39;)
    attempts += 1

while count &lt; len(guess):
    if guess [count].lower() not in word:
        print(&#39;_&#39;, end=&#39;&#39;)
    elif guess [count].lower() == word[count]:
        print(guess[count].upper(),end=&#39;&#39;)
    else:
        print(guess[count].lower(),end=&#39;&#39;)
        count += 1
attempts += 1
print()
print(f&#39;it took you {attempts} guesses.&#39;)
print()

答案1

得分: 0

为了实现期望的行为,即循环运行六次并在输入不正确时要求用户重新猜测,您可以按以下方式修改代码:

word = 'flower'
print()
print('提示:')
count = 0
while count < len(word):
    print('_', end='')
    count += 1
print()
print()

attempts = 0 
guess = input('您猜的是什么词? ')
count = 0
while len(guess) != len(word):
    guess = input('请输入长度正确的单词: ')
    attempts += 1

while count < len(guess):
    if guess[count].lower() != word[count]:
        break
    count += 1

if count == len(word):
    print('恭喜!您猜对了。')
else:
    print('猜错了,请再试一次。')

attempts += 1
print(f'您用了 {attempts} 次猜测。')
print()

在这个修改后的代码中:

在提示用户猜测后,我们检查猜测的长度是否与单词的长度匹配。如果不匹配,我们将重复要求用户输入具有正确长度的单词,直到提供有效的猜测为止。attempts 变量用于跟踪尝试的次数。

在检查每个猜测字符的循环中,如果猜测中的任何字符不正确,我们会跳出循环。如果循环完成而没有跳出,意味着整个猜测与单词匹配,我们将打印祝贺消息。否则,我们会指示猜测不正确,并提示用户再次尝试。

最后,我们增加了 attempts 变量,它计算了总共尝试的次数,并打印出总尝试次数。

通过使用 break 语句,每当在猜测中遇到不正确的字符时,循环将重新从头开始,确保用户有机会再次输入正确的猜测。

英文:

To achieve the desired behavior of running the loop six times and asking the user to guess again if the input is incorrect, you can modify the code as follows:

word = &#39;flower&#39;
print()
print(&#39;Hint:&#39;)
count = 0
while count &lt; len(word):
    print(&#39;_&#39;, end=&#39;&#39;)
    count += 1
print()
print()

attempts = 0 
guess = input(&#39;What is your guess? &#39;)
count = 0
while len(guess) != len(word):
    guess = input(&#39;Please enter a word with the correct length: &#39;)
    attempts += 1

while count &lt; len(guess):
    if guess[count].lower() != word[count]:
        break
    count += 1

if count == len(word):
    print(&#39;Congratulations! Your guess is correct.&#39;)
else:
    print(&#39;Incorrect guess. Please try again.&#39;)

attempts += 1
print(f&#39;It took you {attempts} guesses.&#39;)
print()

In this modified code:

After prompting the user for a guess, we check if the length of the guess matches the length of the word. If not, we repeatedly ask the user to enter a word with the correct length until a valid guess is provided. The attempts variable keeps track of the number of attempts.

Inside the loop that checks each character of the guess, we break out of the loop if any character in the guess is incorrect. If the loop completes without breaking, it means the entire guess matches the word, and we print a congratulatory message. Otherwise, we indicate an incorrect guess and prompt the user to try again.

Finally, we increment the attempts variable, which counts the total number of guesses made, and print the total number of attempts.

By using the break statement, the loop will restart from the beginning whenever an incorrect character is encountered in the guess, ensuring that the user gets another chance to enter the correct guess.

答案2

得分: 0

以下是您要翻译的代码部分:

word = 'flower'

wordLength = len(word)

count = 0
while count <= wordLength-1:
    askUserInput = input("Guess the word: ")

    if askUserInput == word:
        print("Congratulations, you have guessed the correct word")
        break

    print("Wrong guess, please try again")

    count += 1

if count == wordLength:
    print("All chances have been finished to guess the word")

希望这对您有所帮助!

英文:

You want a program which asks user to guess a word for the length of the word number of times and if the users guesses it before then it would stop asking and print a congratulating message.

word = &#39;flower&#39;

wordLength = len(word)

count = 0
while count &lt;= wordLength-1:
    askUserInput = input(&quot;Guess the word: &quot;)

    if askUserInput == word:
        print(&quot;Congratulations, you have guessed the correct word&quot;)
        break

    print(&quot;Wrong guess, please try again&quot;)

    count += 1

if count == wordLength:
    print(&quot;All chances have been finished to guess the word&quot;)

Here the while loop will keep running until it the count variable matches the length of the word and the guesses made by user is wrong. But before reaching its limit, if the user guesses the right word then the loop breaks and prints a Congratulation message. Hope it helps While循环 – len(Words)循环六次,“flower”有六个字母。

huangapple
  • 本文由 发表于 2023年5月17日 10:15:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定