解析时较高或较低的游戏意外EOF

2024-03-28 15:17:57 发布

您现在位置:Python中文网/ 问答频道 /正文

需要更高或更低游戏的帮助我认为问题与循环有关。我被告知要添加一个例外,但我不知道在哪里添加它

print('Welcome to higher or lower game')

input('press enter to start.\n')

import random
Max = 10
Min = 0

num = random.randint(1, 10)

print('your starting number is a ' + str(num))

while 'well done.\n' :

guess = input('higher (h) or lower (l).\n')

new_num = random.randint(1, 10)

print('your new number is a ' + str (new_num))

try :
   if new_num > num and guess == 'h':
    print('well done.\n')
   elif new_num < num and guess == 'l':
    print('well done.\n')
    break

  if num and guess == 'l' and new_num > num and guess:
   print('game over')
  elif num and guess == 'h' and new_num < num and guess:
   print('game over')
  else:
   print('game over you got a score of ' + str(score))  

Tags: orandtogamenewrandomlowernum
2条回答

你真的不应该在那里有一个try语句。你可以把它拿出来,用一些if和elif语句

例如:

import random
number = random.randint(1,10)
lives = 3
Success = False
while lives > 0:
    guess = int(input("What is your guess between 1 and 10? \r\n"))
    if guess > number:
        print("Too high! Go lower. \r\n")
        lives -= 1
    elif guess < number:
        print("Too low! Go higher. \r\n")
        lives -= 1
    elif guess == number:
        print("Congratulations, you win!")
        global Success = True
        break
if Success != True:
    print("Sorry. Try again!  The number was ", number, ".")

据我所知,try语句主要用于错误处理

try语句中没有except子句。除非您有finally子句,否则该子句是必需的

相关问题 更多 >