为什么此代码不返回任何内容?

时间:2018-08-18 16:01:39

标签: python-3.x

我当时正在用python编写一个猜谜游戏,但我的计算机崩溃了,现在代码无处不在,现在我不确定每个块的最终结果应该是它沿线输出了什么关闭

你好!你叫什么名字?

阿尔伯特

阿尔伯特,我想的是1到20之间的数字。

猜一猜。

10

您的猜测太高了。

猜一猜。

2

您的猜测太低了。

猜一猜。

4

干得好,阿尔伯特!您猜了我3个数字!

print('Hello! What is your name?')

myName = input()
number = random.randint(1, 20)

print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')


while guessesTaken < 6:

print('Take a guess.') # There are four spaces in front of print.

guess = input()

 guess = int(guess)



 guessesTaken = guessesTaken + 1


 if guess < number:
   print('Your guess is too low.') # There are eight spaces in front of print.



 if guess > number:

    print('Your guess is too high.')


 if guess == number:

     break


if guess == number:
 guessesTaken = str(guessesTaken)
 print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken 
  + ' guesses!')





 if guess != number:

number = str(number)

 print('Nope. The number I was thinking of was ' + number)

1 个答案:

答案 0 :(得分:0)

我正确地缩进了代码,添加了一行(以初始化guessesTaken),现在看来可以正常工作了:

print('Hello! What is your name?')

myName = input()
number = random.randint(1, 20)

print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')

guessesTaken=0
while guessesTaken < 6:

   print('Take a guess.') # There are four spaces in front of print.
   guess = input()
   guess = int(guess)
   guessesTaken = guessesTaken + 1

   if guess < number:
       print('Your guess is too low.') # There are eight spaces in front of print.

   if guess > number:
       print('Your guess is too high.')

   if guess == number:
       break

if guess == number:
    guessesTaken = str(guessesTaken)
    print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number:
   number = str(number)    
   print('Nope. The number I was thinking of was ' + number)