gpt4 book ai didi

python - 刽子手 |使用名字和姓氏作为 "word"的问题

转载 作者:行者123 更新时间:2023-12-04 07:27:05 25 4
gpt4 key购买 nike

我是编程新手。我正在做一个经典的刽子手游戏,但我希望它是从节目中猜测一些角色的名字(名字和姓氏)。
我的问题:破折号打印 name 和 surname 之间的空格而不是忽略它,例如:
密语:鲁伯特·吉尔斯
猜出所有字母后如何打印:Rupert_Giles
(是的,破折号后面有一个空格)
我试过:

  • 拆分()
  • 写字符而不是字母(因为空格不算数
    作为第 41,42 和 43 行中的字符)
  • 将名称分隔为 ["Name""Surname,
    "姓名 2""姓氏 1"]
  • 和 ['Name'' ''Surname','Name1'' ''Surname1']

  • 我认为是什么问题: secret “单词”存储在main中,main负责放置破折号而不是字母,该部分的代码是这样的
    main = main + "_" + " "
    所以也许那里应该有一些东西应该改变,所以它忽略了空格

    我的代码:
    import random, sys, time, os


    def typingPrint(text):
    for character in text:
    sys.stdout.write(character)
    sys.stdout.flush()
    time.sleep(0.05)


    def typingInput(text):
    for character in text:
    sys.stdout.write(character)
    sys.stdout.flush()
    time.sleep(0.05)
    value = input()
    return value


    def clearScreen():
    if os.name == 'posix':
    _ = os.system('clear')
    else:
    _ = os.system('cls')


    def hangman():
    word = random.choice(
    ["Spike", "Rupert Giles", "Tara Maclay", "Willow Rosenberg", "Buffy Summers", "Xander Harris",
    "Wesley Wyndam Price", "Faith Lehane", "Angel", "Darla", "Cordelia Chase", "Dawn Summers",
    "Anya Jenkins", "Riley Finn", "Drusilla", "Oz Osbourne", "Kendra Young", "Joyce Summers", "Master",
    "Harmony Kendall",
    "Jenny Calendar", "Robin Wood", "Jonathan Levinson",
    "Ethan Rayne", "Principal Snyder", "Kennedy", "Veruca", "Hank Summers", "Halfrek", "DHoffryn", "Mr Tick"])
    validLetters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    turns = 10
    guessmade = ''
    while len(word) > 0:
    main = ""
    missed = 0
    for character in word:
    if character in guessmade.lower() or character in guessmade.upper():
    main = main + character
    else:
    main = main + "_" + " "
    if main == word:
    print(main)
    typingPrint("Congratulations! You win... for now.\nLet's try again")
    typingPrint("\nin 3...2...1")
    clearScreen()
    break

    print("\nGuess the word:", main)
    guess = input()

    if guess in validLetters:
    guessmade = guessmade + guess
    else:
    typingPrint("Enter a valid character: ")
    guess = input()
    if guess.lower() not in word and guess.upper() not in word:
    turns = turns - 1
    if turns == 9:
    typingPrint("You have 9 turns left")
    print("\n -------- this is a ceiling, don't laugh at it")
    if turns == 8:
    typingPrint("You have 8 turns left")
    print("\n -------- ")
    print(" O ")
    if turns == 7:
    typingPrint("You have 7 turns left")
    print("\n -------- ")
    print(" O ")
    print(" | ")
    if turns == 6:
    typingPrint("You have 6 turns left")
    print("\n -------- ")
    print(" O ")
    print(" | ")
    print(" / ")
    if turns == 5:
    typingPrint("You have 5 turns left")
    print("\n -------- ")
    print(" O ")
    print(" | ")
    print(" / \ ")
    if turns == 4:
    typingPrint("You have 4 turns left")
    typingPrint("\nYou are walking on thin ice")
    print("\n -------- ")
    print(" \ O ")
    print(" | ")
    print(" / \ ")
    if turns == 3:
    typingPrint("You have 3 turns left")
    typingPrint("\n No pressure")
    print("\n -------- ")
    print(" \ O / ")
    print(" | ")
    print(" / \ ")
    if turns == 2:
    typingPrint("You have 2 turns left")
    typingPrint("\nYou call yourself a Buffy fan?")
    print("\n -------- ")
    print(" \ O / | ")
    print(" | O ")
    print(" / \ ")
    if turns == 1:
    typingPrint("You have 1 turn left")
    typingPrint("\nYou should re-watch all seasons")
    print("\n -------- ")
    print(" \ O_|/ ")
    print(" | ")
    print(" / \ ")
    if turns == 0:
    typingPrint("You lose, ")
    typingPrint(name)
    typingPrint("\nThe name was...")
    typingPrint(word)
    print("\n -------- ")
    print(" O_| ")
    print(" /|\ ")
    print(" / \ ")
    typingPrint("Good night")
    typingPrint("\nin 3...2...1")
    clearScreen()
    break


    while 1:
    name = typingInput("Enter your name: ")
    typingPrint("Welcome, ")
    typingPrint(name)
    typingPrint(". You have been fooled\n")
    typingPrint("Now you are my prisioner, my dear ")
    typingPrint(name)
    typingPrint("\nMwahahaha")
    print("\n-------------------------------------")
    typingPrint("Try to guess the name of this Buffy's character in less than 10 attempts")
    typingPrint("\nIf you can't, you will be hanged")
    typingPrint("\nNo pressure, ")
    typingPrint(name)
    typingPrint("\nOh... and one little, itty bitty thing...")
    typingPrint("\nThe names are from Season 1 to 7")
    typingPrint("\nGood luck, break a neck... I mean, a leg")
    hangman()
    print()

    最佳答案

    您可以进行此调整。代替

    if character in guessmade.lower() or character in guessmade.upper():
    main = main + character
    if character in guessmade.lower() or character in guessmade.upper() or character == " ":
    main = main + character
    就目前而言,您希望您的用户猜测一个空格字符。这个新的 if 语句消除了这个要求。

    关于python - 刽子手 |使用名字和姓氏作为 "word"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68156077/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com