RobbyC 发表于 2020-8-29 10:30:23

print无法打印正确结果


i = 1
print ('Welcome to our game!')
temp = input ('Please input your number:')
guess = int (temp)
while guess != 8 and i<5:
    if guess == 8:
      print ('Correct! Congraturation')
    else:
      if guess < 8:
            temp = input ('Your number is too small, please try again:')
            guess = int (temp)
            i=i+1
      else:
            temp = input ('Your number is too large, please try again:')
            guess = int (temp)
            i=i+1
print ('Game over')




Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
============================= RESTART: D:\桌面文件\1.py ============================
Welcome to our game!
Please input your number:8
Game over
>>>


这个游戏的运算结果为什么不显示正确的结果,而是直接游戏结束?

sunrise085 发表于 2020-8-29 10:38:38

本帖最后由 sunrise085 于 2020-8-29 10:41 编辑

因为你直接输入8,根本就进不了while循环
帮你修改了一下
i = 1
print ('Welcome to our game!')
temp = input ('Please input your number:')
guess = int (temp)
while guess != 8 and i<5:
    if guess == 8:
      break#结果对了的话,就跳出循环
    else:
      if guess < 8:
            temp = input ('Your number is too small, please try again:')
            guess = int (temp)
            i=i+1
      else:
            temp = input ('Your number is too large, please try again:')
            guess = int (temp)
            i=i+1
if guess == 8:#循环结束后,若结果对的话,就是print
    print ('Correct! Congraturation')
print ('Game over')

匆匆又夏天 发表于 2020-8-29 10:41:30

你while写了不等于8才能进入循环 你直接输入8进不了while的循环,print ('Correct! Congraturation')这句就不会打印。

昨非 发表于 2020-8-29 10:43:14

i = 1
print ('Welcome to our game!')
temp = input ('Please input your number:')
guess = int (temp)
while i<5:
    if guess == 8:
      print ('Correct! Congraturation')
      break
    elif guess < 8:
      temp = input ('Your number is too small, please try again:')
      guess = int (temp)
      i=i+1
    else:
      temp = input ('Your number is too large, please try again:')
      guess = int (temp)
      i=i+1
print ('Game over')


你的判断条件必须满足【输入不等于8且i<5】才能进入循环,也就是你输入正确答案8的时候无法进入循环,直接结束了

RobbyC 发表于 2020-8-29 11:22:21

sunrise085 发表于 2020-8-29 10:38
因为你直接输入8,根本就进不了while循环
帮你修改了一下

十分感谢!

RobbyC 发表于 2020-8-29 11:23:12

匆匆又夏天 发表于 2020-8-29 10:41
你while写了不等于8才能进入循环 你直接输入8进不了while的循环,print ('Correct! Congraturation')这句就 ...

懂了!感谢!
页: [1]
查看完整版本: print无法打印正确结果