【问题标题】:How do I do if word_pick not == random_choice?如果 word_pick 不是 == random_choice,我该怎么办?
【发布时间】:2020-11-12 13:52:15
【问题描述】:
import random
import time
import sys

from colored import fg

color1 = fg("red")
color2 = fg("blue")
color3 = fg("green")
color4 = fg("yellow")


for x in (color1 + "Hi,\nThis is a word guessing game, you will have to guess the answer from 100 words.\nYou will have 45 chances to guess the number. You initially start with 225 points. Every chance you enter the wrong answer, you lose 5 points. The quicker you guess, the higher your score(225 is the highest score you can have).\n\n"):
    sys.stdout.write(x)
    sys.stdout.flush()
    time.sleep(0.03)

words = ['time', 'year', 'people', 'way', 'day', 'man', 'thing', 'woman', 'life', 'child', 'world', 'school', 'state', 'family', 'student', 'group', 'country', 'problem', 'hand', 'part', 'place', 'case', 'week', 'company', 'system', 'program', 'question', 'work', 'government', 'number', 'night', 'point', 'home', 'water', 'room', 'mother', 'area', 'money', 'story', 'fact', 'month', 'lot', 'right', 'study', 'book', 'eye', 'job', 'word', 'business', 'issue', 'side', 'kind', 'head', 'house', 'service', 'friend', 'father', 'power', 'hour', 'game', 'line', 'end', 'member', 'law', 'car', 'city', 'community', 'name', 'president', 'team', 'minute', 'idea', 'kid', 'body', 'information', 'back', 'parent', 'face', 'others', 'level', 'office', 'door', 'health', 'person', 'art', 'war', 'history', 'party', 'result', 'change', 'morning', 'reason', 'research', 'girl', 'guy', 'moment', 'air', 'teacher', 'force', 'education']


for x in (color2+ f"These are the words in the list you can pick from:\n\n{color3}'time', 'year', 'people', 'way', 'day', 'man', 'thing', 'woman', 'life', 'child', 'world', 'school', 'state', 'family', 'student', 'group', 'country', 'problem', 'hand', 'part', 'place', 'case', 'week', 'company', 'system', 'program', 'question', 'work', 'government', 'number', 'night', 'point', 'home', 'water', 'room', 'mother', 'area', 'money', 'story', 'fact', 'month', 'lot', 'right', 'study', 'book', 'eye', 'job', 'word', 'business', 'issue', 'side', 'kind', 'head', 'house', 'service', 'friend', 'father', 'power', 'hour', 'game', 'line', 'end', 'member', 'law', 'car', 'city', 'community', 'name', 'president', 'team', 'minute', 'idea', 'kid', 'body', 'information', 'back', 'parent', 'face', 'others', 'level', 'office', 'door', 'health', 'person', 'art', 'war', 'history', 'party', 'result', 'change', 'morning', 'reason', 'research', 'girl', 'guy', 'moment', 'air', 'teacher', 'force', 'education' \n\n\n"):
    sys.stdout.write(x)
    sys.stdout.flush()
    time.sleep(0.01)

time.sleep(2)

word_pick = input(color4 + "Pick a random word from the above list: ")

random_choice = random.choice(words)

chances = 45
score = 225

if word_pick.upper() or word_pick.lower() or word_pick.title() not random_choice:
    print("Congrats, you have picked the right word.")

if word_pick.upper() or word_pick.lower() or word_pick.title() == random_choice:
    print("Congrats, you have picked the right word.")

我说的是第一个 if 语句,我怎么不 == random_choice ??? 所以我想如果 word_pick 不在 random_choice 中,则将分数减去 5

请帮助

【问题讨论】:

    标签: python-3.x random guess.js


    【解决方案1】:

    我建议要么只检查单词的小写版本,要么严格检查。

    PENALTY = 5
    if word_pick.lower() == random_choice:
        # guess is right
        print("Congrats")
    else:
        # guess isn't right
        print("You have failed")
        score -= PENALTY
    

    如果您想单独测试所有可能的版本,可以将它们放入一个列表并使用in 运算符。

    if random_choice in [word_pick.lower(), word_pick.upper(), word_pick.title()]:
        # guess is right
    

    【讨论】:

      【解决方案2】:

      使用 !=

      if word_pick.upper() or word_pick.lower() or word_pick.title() != random_choice:
          print("Congrats, you have picked the right word.")
          score = score - 5
      

      在这里阅读更多: https://docs.python.org/3/library/stdtypes.html#comparisons

      【讨论】:

        猜你喜欢
        • 2022-07-25
        • 2016-01-20
        • 1970-01-01
        • 2022-06-11
        • 1970-01-01
        • 2015-09-07
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多