Python2.7单词猜测

Python2.7单词猜测,python,python-2.7,Python,Python 2.7,我正在尝试用Python2.7编写一个猜字程序。用户有50次机会正确猜出单词。我已经编写了一个函数,可以从单词列表中生成一个随机单词。用户需要正确猜测单词 例如,如果正确的单词是“dog”,用户猜测“apple”,程序将返回“太低”,然后用户将再次猜测。如果用户猜到“pear”,程序将返回“过高” 我真的不知道如何开始写这段代码。我试着用了一个while循环,但我不知道接下来该怎么做。提前谢谢大家 以下是我目前掌握的情况: 注意:“correct”是用户试图猜测的单词;guess是用户猜测的单词

我正在尝试用Python2.7编写一个猜字程序。用户有50次机会正确猜出单词。我已经编写了一个函数,可以从单词列表中生成一个随机单词。用户需要正确猜测单词

例如,如果正确的单词是“dog”,用户猜测“apple”,程序将返回“太低”,然后用户将再次猜测。如果用户猜到“pear”,程序将返回“过高”

我真的不知道如何开始写这段代码。我试着用了一个while循环,但我不知道接下来该怎么做。提前谢谢大家

以下是我目前掌握的情况:

注意:“correct”是用户试图猜测的单词;guess是用户猜测的单词

while guess != 'correct':
    if guess < correct:
        print 'Too low'
        guess = int(raw_input('Enter a word')
    elif guess > correct:
        print 'Too high'
        guess = int(raw_input('Enter a word')
    else:
        print 'You finally got it!'
        break
    print

while tries < 50:
    guess = raw_input('Guess a word')
    if guess not in word: 
        print "Sorry, try again."
    else:
        print 'Good job!'
else:
    print "Sorry. My word was ", word, ". Better luck next time!"
while guess!='正确':
如果猜测<正确:
打印“太低”
guess=int(原始输入('输入单词')
elif guess>正确:
打印“太高”
guess=int(原始输入('输入单词')
其他:
打印“你终于得到了!”
打破
打印
在小于50的情况下:
guest=原始输入('猜猜一个单词')
如果猜测不在文字中:
打印“对不起,再试一次。”
其他:
打印“干得好!”
其他:
打印“对不起,我的话是”,字,“祝你下次好运!”

不需要两个循环。您可以使用如果一个人猜对了单词,或者这个人已经没有生命了,这将停止您的循环。您的输入也不需要说int(),因为您只输入了一个单词。如果单词“太低”你把它打印出来,然后输入一个新词,就像“太高”一样。当你的生命值为0或者你猜到了这个词时,你就停止了

guess = raw_input('Guess a word: ')
correct = 'dog'
lives = 50

while guess != correct and lives != 0:
    if guess < correct:
        print 'Too low'
        lives -= 1
        guess = raw_input('Enter a word: ')
    else:
        print 'Too high'
        lives -= 1
        guess = raw_input('Enter a word: ')


if lives == 0 and guess != correct:
    print "Sorry. My word was", correct,". Better luck next time!"
else:
    print "You finally got it!"
guess=raw_输入('guess a word:')
正确='dog'
寿命=50
而猜测!=正确和生命!=0:
如果猜测<正确:
打印“太低”
寿命-=1
猜测=原始输入('输入单词:')
其他:
打印“太高”
寿命-=1
猜测=原始输入('输入单词:')
如果lives==0且QUOTE!=正确:
打印“对不起,我的话是”,正确,“祝你下次好运!”
其他:
打印“你终于得到了!”
我喜欢回答这个问题。这里是OmO代码的一个细微变化

# I like adding a .lower() so the person doesn't have to worry about the case matching.
correct = 'dog'.lower()
lives = 2

guess = None

# I like doing a > check not an != because stuff happens and if lives become < 0 you
# get stuck in an infinite loop. BUT in some cases comparisons <, > can be slower than ==.
# So if speed is a problem stick to ==
while not guess == correct and not lives <= 0:
    # Moving this here makes it so there are fewer places you have to get input.

    # Prints how many lives the user has left.
    print "{} lives remaining".format()
    guess = raw_input('Guess a word: ').lower()

    if guess < correct:
        print '{} was too low'.format(guess)
    elif guess > correct:
        print '{} was too high'.format(guess)
    lives -= 1

if not lives > 0 and not guess == correct:
    # I like using the format string method instead of adding strings or using ',' in a print. It is more reliable if
    # the variable you are trying to print is not a string.
    print "Sorry. My word was {}. Better luck next time!".format(correct)

# Makes sure the guess is really right before giving the confirmation
elif guess == correct:
    print "You got it! The word was {}".format(correct)

# This is an added safety that will be triggered if some how the other conditions fall through. Its better to
# have an error thrown than have a program silently break.
else:
    print "ERROR. Something is broken"
#我喜欢添加一个.lower(),这样用户就不必担心大小写匹配问题。
correct='dog'.lower()
寿命=2
猜测=无
#我喜欢做一个>检查而不是一个!=因为事情发生了,如果生活变得<0你
#陷入无限循环。但在某些情况下,比较可能比==慢。
#因此,如果速度是一个问题,坚持==
而非猜测==正确且不正确:
打印“{}”太高。格式(猜测)
寿命-=1
如果未生存>0且未猜测==正确:
#我喜欢使用格式化字符串方法,而不是在打印中添加字符串或使用“,”。如果
#您试图打印的变量不是字符串。
打印“对不起,我的单词是{}。祝你下次好运!”。格式(正确)
#在确认之前确保猜测是正确的
elif guess==正确:
打印“明白了!单词是{}”。格式(正确)
#这是一个额外的安全性,如果其他条件发生变化,它将被触发
#抛出错误比让程序以静默方式中断要好。
其他:
打印“错误,有东西坏了”

我不确定你将单词相互比较为“太高”或“太低”是什么意思,请不要将你的代码放在imagesHi Austin中。我理解这可能会令人困惑。我所说的“太高”是指单词超出了字母表中的正确单词。因此,如果正确的单词(我正在尝试猜测的单词)是“猫”,用户会猜测“苹果”,程序会返回“太高”,因为“c”在“a”之后,太高了这更有意义吗?暂时忘掉python和编程吧——回到简单的古英语。你到底想让你的应用程序做什么?为什么苹果比狗少,为什么梨比狗多?试着把你的目标分解成一系列非常离散的步骤。为什么狗比苹果多,但比梨少?在python中,你可以抓住通过使用
ord()
,字符的ASCII值。代码中存在一种边缘情况,即使他们在最后一个循环中猜到了正确的单词,他们仍然会得到“对不起”消息。简单的解决方法是将
if lifes==0
更改为
if lifes==0,然后猜猜!=正确的
肯定是一个更好的代码,我所做的只是玩@hashicode代码,因此他更容易理解。我真的很喜欢你代码中的格式,让事情看起来更好。非常感谢大家,这肯定更有意义。是吗e是否仍要跟踪我使用了多少“生命”?
lifes
变量将跟踪您还剩下多少生命。每次向用户提供猜测时,都会从变量中减去一个,直到
lifes
成功