. 设计一个猜字母的程序,程序随机给出 26 个小写字母中的一个, 答题者输入猜测的字母,若输入的不是 26 个小写字母之一,答题 者输入猜测的字母,若输入的不是 26 个小写字母之一,让用户重 新输入;若字母在答案之前或之后,程序给出相应正确提示;若 答错 5(含)次,则答题失败并退出游戏;若回答正确,程序输出 回答次数并退出游戏。 # 以下代码为提示框架 # 请在...处使用一行或多行代码替换 # 请在______处使用一行代码替换 # # 注意:提示框架代码可以任意修改,以完成程序功能为准 import _____ letter_list = ['a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] letter = letter_list[random._____(0, 25)] count = 0 while True: ...
时间: 2023-08-21 18:06:32 浏览: 494
tj.rar_visual c_数字字符个数
import random
letter_list = ['a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
letter = letter_list[random.randint(0, 25)]
count = 0
guesses = []
while True:
guess = input("Please guess a letter: ")
if len(guess) != 1 or guess not in letter_list:
print("Please enter a valid letter!")
continue
if guess in guesses:
print("You have already guessed this letter!")
continue
guesses.append(guess)
count += 1
if guess == letter:
print("Congratulations! You have guessed the letter {} in {} attempts.".format(letter, count))
break
elif guess < letter:
print("The letter is after {} in the alphabet.".format(guess))
else:
print("The letter is before {} in the alphabet.".format(guess))
if count == 5:
print("Sorry, you have failed to guess the letter within 5 attempts. The letter was {}.".format(letter))
break
阅读全文