python猜数字游戏,求帮忙找错误并改正

#Thisisaguessthenumbergame.importrandomdefaskGuess():guess=input('Takeaguess.Youhave'... # This is a guess the number game.
import random

def askGuess():
guess = input('Take a guess. You have ' + str(6-guessesTaken) + ' tries left: ')

guess = 0
guessesTaken = 0
print('Hello! What is your name?')
myName = raw_input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:

askGuess()

if guess < number:
print('Your guess is too low.')
else :
print('Your guess is too high.')

if guess == number:
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
elif guess != number:
print('Nope. The number I was thinking of was ' + number)

新手,看不出有几处错误,请教如何改正,急!!谢谢!!!
展开
 我来答
neosicifore
推荐于2016-06-30 · TA获得超过2258个赞
知道小有建树答主
回答量:446
采纳率:66%
帮助的人:498万
展开全部
#coding=utf-8
'''
Created on 2014-11-04

@author: Neo
'''
# This is a guess the number game.
import random

def askGuess():
    guess = raw_input('Take a guess. You have ' + str(6-guessesTaken) + ' tries left: ')
    guess = int(guess)
    return guess

guess = 0
guessesTaken = 0
print('Hello! What is your name?')
myName = raw_input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
    guess = askGuess() 
    guessesTaken += 1 
    if guess < number:
        print ('Your guess is too low.') 
    elif guess > number :
        print ('Your guess is too high.')
    else:
        break
    
if guess == number:
    print('Good job, ' + myName + '! You guessed my number in ' + str(guessesTaken) + ' guesses!')
elif guess != number:
    print('Nope. The number I was thinking of was ' + str(number))

结果:

Hello! What is your name?

god

Well, god, I am thinking of a number between 1 and 20.

Take a guess. You have 6 tries left: 10

Your guess is too low.

Take a guess. You have 5 tries left: 15

Your guess is too high.

Take a guess. You have 4 tries left: 12

Your guess is too high.

Take a guess. You have 3 tries left: 11

Good job, god! You guessed my number in 4 guesses!

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式